Weird behaviour of code

Today while trying to solve this problem, I received wrong answer with Java but AC with a similar logic in C++. I have checked answers till 10^5 and they match for both the codes. Even answers for 2*10^7 is the same. Could anyone tell me for which case my Java code is failing, or if the error exists in the input/checker itself?

Hello @tamo11. i code in Java and i checked your code. You are sorting all the numbers to get the smallest possible permutation right? I would like to suggest a better approach. If you check that the digits of N are in increasing order then the number is LPC number. For example: 1223 is LPC, 1323 is not LPC as 2<3. Thus instead of sorting the numbers you can check if they are sorted. This will highly reduce time.

nope still not working wa again

Since it’s an external contest, it’s probably some issue with the input formatting.

Try reading n via

public static void main(String[] args)throws IOException { 
	   Scanner sc = new Scanner(;
		try {
		   int n = sc.nextInt();
		catch (Exception e) {

(taken from this AC Java solution).


thanks a lot, it worked, even though I have no idea what the error actually was

1 Like

I can’t come up with any plausible testcase where your Java solution would WA but your C++ would pass, but here’s an implausible one (note the space after the -):

- 23