I have an array Ar
of size N(Ar[N]
). I have to divide the Ar[N]
into sub arrays of size K
and then add them to a Set S.
For example if Ar[5] = {1,2,3,4,5}
and K = 3
then,
Sub arrays will be {1,2,3},{2,3,4},{3,4,5}
Now I have to add these sub arrays to the Set and continue my computations.
Here is my code :
int max = 0;
for(int j=0;j<=N-k;j++){
Set<Integer> set = new HashSet<>();
for(int l=j;l<j+K;l++){
set.add(Ar[l]);
}
if(set.size >= max) max = set.size;
}
System.out.println(max);
Is there any way that I can get rid of the nested for loop as in worst case N can be 1000000.
NOTE : The set should only contain unique values.
Hope I am clear.