Why do I get a sigsegv in this program?

The first five test cases are running fine but I am getting a SIGSEGV in others. Please help.

Link for problem : https://www.codechef.com/ZCOPRAC/problems/ZCO1
Here’s my code for the same:(gcc 6.3 c compiler)

https://www.codechef.com/viewsolution/19832390