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

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

Check for this input

**1**

**10**

**1 4 7 4 5 8 4 1 8 6**

Your output - 1474584816

**Correct output -1474584618**

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

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**

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**

my output here also is 15834 only

but now for 5 4 3 2 1 it gives 21345

thanks for constantly helping me

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