sábado, 9 de julho de 2022

PermCheck - Codility

permutation is a sequence containing each element from 1 to N once, and only once.

A set of integers will be a permutation if there are no duplicates or values bigger then the sequence. 


import java.util.Arrays;

class Solution { public int solution(int[] A) { // write your code in Java SE 8 int size= A.length; int [] holder= new int [size+1]; Arrays.fill(holder, 0); for (int t=0; t<size; t++){ if (A[t]>size) return 0; if(holder[A[t]]==0){ holder[A[t]]=A[t]; }else return 0; } return 1; } }

Sem comentários:

Enviar um comentário