CNDYGAME help

PLZZ TELL WHERE AM I GETTING WRONG
I HAVE LOOKED AFTER EVERY EDGE CASES AND ALSO SEEN THE VIDEO EDITORIAL AND I CAN SEE I HAVE NOT MISSED A SINGLE EDGE CASE

question : CodeChef: Practical coding for everyone
my solution : CodeChef: Practical coding for everyone
editorial : CNDYGAME - Editorial
if somebody could point out the mistake than it will be very helpfull

I had a similar case where I only passed the last test case - someone pointed out that I missed this test case - maybe it’s the same for you?

N =5
1 2 3 4 5
Q=5
r=7
answer should be 2