Can't Figure out why answer is wrong: 1238B

Revision en2, by thebigjuicyd, 2020-07-20 19:20:29

I was doing 1238B: Kill 'Em All and this was my solution: ~~~~~ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for(int t = 0; t<T; t++){ int n = sc.nextInt(); int r = sc.nextInt(); Integer arr[] = new Integer[n]; for(int i = 0; i<n; i++)arr[i] = sc.nextInt(); Arrays.sort(arr); int cnt = 0; for(int i = n — 1; i >= 0; i--){ if(arr[i] — cnt * r <= 0)break; else{ cnt++; while(i > 0 && arr[i — 1] == arr[i])i--; } } out.println(cnt); } out.flush(); ~~~~~ However, with the way that I did it, the only difference between my solution and the working one is the while loop at the end that goes through values that are the same in the array. I just don't understand why my while loop gets an error while Dukkha's doesn't(has solution that is identical but with a different while loop a the end). Dukkha's Solution: 62167923 Thanks!

Tags #sortings, 1238b

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English thebigjuicyd 2020-07-20 19:22:38 0 (published)
en4 English thebigjuicyd 2020-07-20 19:22:00 897
en3 English thebigjuicyd 2020-07-20 19:21:08 290
en2 English thebigjuicyd 2020-07-20 19:20:29 630 Tiny change: ' solution:\n[submissio' -> ' solution:[submissio'
en1 English thebigjuicyd 2020-07-20 19:17:27 517 Initial revision (saved to drafts)