Piyush and magical park

What is wrong in my code? My code is https://ide.codingblocks.com/s/347839

I believe you are adding too many unnecessary checks, please see this code which has same logic as yours