PCYCLE: Need help ( runtime error[SIGABRT] )

I tried a lot to solve this question. Every time I am getting runtime error(SIGABRT). I even tried for large inputs, i.e., N=1000. It is working perfectly. Here is the ideone link for large inputs, i.e., N=1000 PCYCLE.cpp. I tried to view some solutions of this questions, but I am not able to figure out what is wrong with my code. Someone please help me in understanding where I am going wrong.

I am new to this asking forum. So please adjust with the way I asked the question.