Need help with O(N) solution for SHIVIGOD

Dear friends,

What is wrong with this solution for SHIVIGOD?


Please help. I have been stuck for a while,
The example test case passes but my solution gives a WA
https://www.codechef.com/viewsolution/40453332