Help JNEXT

I am trying to solve this but i am getting WA everytime. I have used the next_permutation as well as the
algorithm but still am getting WA…Help…!!!

link to the question : http://www.spoj.com/problems/JNEXT/
link to my answer : https://ideone.com/uH0ujN

This is your Ac solution: https://ide.geeksforgeeks.org/OgWyqez21V
You were actually printing the function also which caused an additional 0 to be appended to the ans.

1 Like

Don’t use next permute. it will give a wrong answer in the test case 1
5
5 4 3 0 1
as the function “next permutation” gives the lexicographically next permutation. but will not work on this case. You can just apply a for loop in reverse and check for any element where a[i] > a[i-1], swap these two and sort from i to n. this will give you your required result.

Regards.

Why would it not work.

The ans for this would be 1554310 which is right i guess.

Thanks it worked…!!

help please, my solution is giving WA: https://ideone.com/6j9k6S