Next permutation

what is wrong now?
we have to tell the next no.?right?
eg.
input - 45293
output - 45329??

hello @sr7.shubhamrai

ss

here this part is not correct.
u need to ensure that array [j…] onwards are in decreasing order.

refer this->

  • Scan from right to left, find the first element that is less than its previous one.
4 5 6 3 2 1 
  |
  p
  • Scan from right to left, find the first element that is greater than p.
4 5 6 3 2 1 
    |
    q
  • Swap p and q
4 5 6 3 2 1 
swap
4 6 5 3 2 1 
  • Reverse elements [p+1, nums.length]
4 6 1 2 3 5 

if the no. is 456312 then next permutation should be 456321 or 461235?

456321
…
…
…
…
…

sorry for last doubt , i understood my mistake now … thank you

can you give me an example where my code is giving wrong answer, like it’s still wrong even after changing

4 5 2 9 3----- what should be the output – 45 3 2 9 or 4 5 9 3 2

4 5 3 2 9
…
…
…
…

can you give me an example where my code is giving wrong answer, like it’s still wrong even after changing

try this->
1
5
1 4 5 3 2

can you tell what is wrong now?

though finaaly i have used the stl function next_permutation(), it’s submitted … but still can u tell what was wrong in the last code that i sent

u need to ensure that [j…n-1] must remain sorted order in decreasing order. which is not handled in ur code

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.