I made the following changes and got the right answer. import java.io.*; import java.lang.*; import java.util.Scanner; import java.util.Arrays; class A { public static void main(String args[])throws IOException { Scanner input = new Scanner(System.in); int t = input.nextInt(); int x; for(x=1;x<=t;x++){ int n = input.nextInt(); int k = input.nextInt(); int a[]=new int[n]; int i; for(i=0;i<n;i++) a[i] = input.nextInt(); Arrays.sort(a); int max=0,index=0; for(int g=0;g<a[n-1];g++){ if(index==n){break;}
//changes made here int h=1; while(h<=k){ if(index==n){break;} if(g<a[index]){index++;max++;h++;} else{index++;} } } System.out.println("Case #"+x+": "+max); } } } Once again Thanku everyone for helping. -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-code+unsubscr...@googlegroups.com. To post to this group, send email to google-code@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/google-code/06642cf4-5b4e-4118-bea0-e035be3f7d67%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.