WA in RPDRDNG [Solved]

Can someone please help me out? I cannot understand why my solution is giving WA. It passes all the sample cases. Where is the flaw?

Question Link : Contest Page | CodeChef

My solution : Solution: 54435899 | CodeChef

Edit 1 : After @aryanc403 pointed it out, I realized the solution fails for
1 2
So, I fixed it, but I’m still getting WA. :-((

Here’s the updated solution : Solution: 54446804 | CodeChef

Any ideas would be most helpful! Thank you.

Edit 2: Solved! Turns out the if-statement had to be bracketed. Learnt a big lesson today - never mess with syntax… :pensive: Its much safer to write properly bracketed and indented code. Cost me a lot in yesterday’s contest.

Special thanks to @uttaran_das for pointing out the Test Case which prompted me to run a complete line-by-line debug. Also, to @aryanc403 for pointing out that base case error.

Thanks a lot guys!

Here is the correct solution : Solution: 54448138 | CodeChef

Moral of the story : Never mess with syntax … :smiley:

Try this test case:

3 1 2 4 2 1 2 4



An what is wrong with this?

Fails on -
1 2

There can be other bugs as well.

Yes! Thanks a lot. But it fails even after I fixed it… :confused:

Updated Solution : Solution: 54446804 | CodeChef

Thanks a lot !

the answer to this should be -1 right?