Debugging help for KPAL (startes 77)

I submitted the code with the same logic given in the editorial. I am failing on second test case for the KPAL problem.

Here is my soluchan ::: CodeChef | Competitive Programming | Participate & Learn

I am not able to find error in my soluchan.

I also generated random input for 1000 arrays (twice). find it here … eik8vQ - Online IDE & Debugging Tool -

Please someone help me find an error, as I am out of option after generating random input and comparing with correct output.