Why this solution gives wrong answer for PCYCLE problem

Please explain where the code fails.Pcycle

Initialize length to 0 first. Otherwise it stores junk value initially.

As you will notice your length is the only thing wrong.