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.

1 Like

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

Getting Wrong Answer, please help!

Thanks in advance!

Why would it not work.

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

Thanks it worked…!!

if we keep on checking only the adjacent elements and swap if the right element is greater than the one at its left and then sorting it, it wont satisfy the test case

3
6 8 7

What will be the solution for this…??
Also could you please elaborate the logic used for the given question without using the next_permutation approach…?

1 Like

Can anyone tell me why am i getting runtime error(sigsegv) in this https://ideone.com/VTJJ5B

This might help you :- https://www.youtube.com/watch?v=quAS1iydq7U
At least it helped me!

try coding the algorithm rather than using inbuilt functions


what wrong with the code?