I tried of all the test cases I could think, it gives right answer but on submission all the test cases fail. (Apart from the fact that the logic and implementation) could have been better can someone please identify why this program is failing ?

Can you please explain your logic which you tried to implement.?

According to your algo i think you should subtract n*(N+1)/2- given sum -1 just because in a valid rank list each number is atleast 1 and not zero …

First I count the sum of series’z’ till the given no ‘n’ using n*(n+1)/2 and since given ‘s’ can not be greater than z, so I calculated the difference between s and z. Then I started subtracting this difference from the largest elements i.e from the end. Then i counted how many elements have changed and that was the answer.

I know this logic could be implemented in a better and now I did it, but I am unable to figure out why this is giving wrong answer when all the test cases show right answers.

I am sorry but my answer might not be clear… The basic thing I think wrong with your code is we cannot make any rank 0 and i think you are doing that