Just next(jnext) spoj problem using stack

Hello , I have been trying to solve this problem spoj jnext using stack
my approach was to push into the stack from the end of the array until array element < stack.top()
here’s link to my solution my solution which passes given testcases in the problem
please help me, where am I going wrong

I am not sure but isn’t it just finding next permutation

1 Like

yes it is, but it was under section stack and queue in dsa learners course so I wanted to solve it using stack

please help ssjgz | CodeChef User Profile for Simon St James | CodeChef

Check for this input
1
10
1 4 7 4 5 8 4 1 8 6
Your output - 1474584816
Correct output -1474584618

1 Like

stack solution with comments

this is basically a very classical problem on stack, “finding next smallest/greater to right/left”.

2 Likes

You can see here ,I have solved it without using stack .

2 Likes

thanks for the reply dear and the solution

thanks for the solution

Hi I tried again to solve this problem, and am continuously trying
here is the recent solution
it is still not getting accepted
please tell me where am I wrong

Check for this input
1
5
5 7 7 7 7
Your output - -1
Correct output -75777

1 Like

the ide that I am using gives 75777 only for that input

Check for this input
1
5
1 5 4 8 3
Your output - 15843
Correct output -15834

1 Like

my output here also is 15834 only
but now for 5 4 3 2 1 it gives 21345 :neutral_face:
thanks for constantly helping me :pray:

I finally solved the problem (got accepted) that too using stack
here’s the link to my solution
thank you everyone for help
sharing my solution so it may help others when they get stuck solving, as mostly people on the internet solved it using next permutation or array but not stack
Thanks and regards

1 Like