I was solving the problem 1561C Deep Down Below where I was using Binary Search on Answer. Initially I was getting Wrong Answer on Test Case 14 so I double checked my code but was unable to find any error.
Link to my Submission — https://codeforces.net/problemset/submission/1561/193939724
So after a lot of Wrong Submissions I removed the Try-Catch Block and then I got Runtime Error (Exit Code is 11).
Link to my Submission — https://codeforces.net/problemset/submission/1561/193939801
Then I replaced "out.println ()" with "System.out.println ()" and I again got Wrong Answer on Test Case 14 saying "Answer contains longer sequence [length = 1000], but output contains 750 elements" so at some point my code is going through a runtime error but I am not able to figure it out.
Link to my Submission — https://codeforces.net/problemset/submission/1561/193939951
Link to the Problem — https://codeforces.net/problemset/problem/1561/C
I tried to use Arraylist< Pair> instead [submission:194002572]of long[][] to sort, and got AC. My submission:194002572
Part of my code:
And this is the Error messege of the original submission: (You can see it by adding
e.printStackTrace(System.out);
in the catch block.By searching
Comparison method violates its general contract!
on the internet, I've found the reason of thejava.lang.IllegalArgumentException
:In java7, a valid
Comparator
must satisfy the reflexivity (compare(a, a)==0
), if any comparator violates this conditions (which means it doesn't consider the case whena==b
),Arrays.sort()
method will throwjava.lang.IllegalArgumentException
.Therefore, we can fix the original solution by modify the
compare
method: (AC submission:194005512)Thank you so much for your reply. I really appreciate you taking the time to write such an informative and helpful message.