Next Permutation

The question is on this link
https://hack.codingblocks.com/app/contests/1179/1075/problem

The solution which i tried is here

It doesnt pass all the test cases.I cant find my mistake.Can you help please