I was attempting this question KNPSK Problem - CodeChef getting WA for the same. What’s wrong with the following solution.
https://www.codechef.com/viewsolution/9144714
EDIT:-
I finally came up with this (CodeChef: Practical coding for everyone) solution covering all the cases