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 : SPOJ.com - Problem JNEXT
link to my answer : uH0ujN - Online IDE & Debugging Tool - Ideone.com

This is your Ac solution: Online Compiler and IDE - GeeksforGeeks
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.

2 Likes

help please, my solution is giving WA: 6j9k6S - Online C++ Compiler & Debugging Tool - Ideone.com

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 VTJJ5B - Online C++0x Compiler & Debugging Tool - Ideone.com

This might help you :- Compute The Next Permutation of A Numeric Sequence - Case Analysis ("Next Permutation" on Leetcode) - YouTube
At least it helped me!

try coding the algorithm rather than using inbuilt functions

what wrong with the code?

After doing this a[i]>a[i-1] you need to search for next greater element not print rest of the elements in sorted order.
After you find the next greater element then put rest of numbers in ascending order.
Eg.
154953:
Your approach gives: 159345
But the answer should be:
155349