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 :
link to my answer :

This is your Ac solution:
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 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.



help please, my solution is giving WA:

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

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

This might help you :-
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.
Your approach gives: 159345
But the answer should be: