ArrayList list = new ArrayList(); for(int i=0;i=k){ int brr[] = new int[k]; brr[count] =arr[j]; s.add(brr[count]); count++; j++; } // System.out.println(s.toString()); if(s.size()!=0) list.add(s.size()); } return list ================================================================================== O(n^2) O(n) =================================================================================== first we will take k elements in that I will find distinct count here my sliding window length is k I will take one more loop Each time i will extend window for one more element , i will remove one element at the beginning //al=2 k=3 1, 2,2,3,3,4,5,6,7,7, 8 ==================================================== class Solution { public int longestkSubstr(String s, int k) { // code here int release=0; HashMap hm=new HashMap<>(); int maxlen=-1; int x=0; for(int i=0;ik){ x=hm.get(s.charAt(release)); x--; hm.put(s.charAt(release),x); if(hm.get(s.charAt(release))==0){ hm.remove(s.charAt(release)); } release++; } if(hm.size()==k){ maxlen=Math.max(maxlen,i-release+1); } } return maxlen; } } ========================================================== 2 Sum equal to k ===========================================================