educative.io

Incorrect test case?

For [3, 3, 1, -1, 2], expected output is showing true. I could not understand why.
Starting at 0 index, it will go to 3rd index next where value is -1.
Then it will be stuck in a loop between 2nd index & 3rd index, which is not unidirectional as given in the problem statement.

Hello Nitish,

The output is true for the array [3, 3, 1, -1, 2] because a cycle exists in this array. Starting with the first element, 3, we move to -1. Here, both pointer’s directions will be changed, so we will stop iterating further, start with the second element 3 and move three steps forward to the last element 2. Then, continue by moving two steps forward and return to the second element, 3 again. Here, we find a loop.

1 Like