# There is something wrong with [1,1,-2,1,4]

• As the title above, [1,1,-2,1,4] has loop of 3->4->3->...，and it's a "forward" loop, but the "run code" returns false
I think it's because I mix a "fake" loop 0->1->2->0 with a genuine loop.

• I agree with you. Here is my solution by marked the visited num to zero.
I think we can start from any index of the array. If there exist a forward/backward loop in the array, we should return true.
It's obvious that [1,1,-2,1,4] exist a forward loop 3 -> 4 -> 3.

``````   bool circularArrayLoop(vector<int>& nums) {
if (nums.size() == 0) return false;
int len = nums.size();
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 0) return false;
}
int pos = 0;
bool fwd = nums[0] > 0, lst = true, ret = false;
for (int i = 0; i < len; i++) {
pos = i;
if (nums[pos] == 0) continue;
while (true) {
if (nums[pos] == 0) {
ret |= fwd && lst;
break;
}
int op = pos;
lst = fwd;
fwd = nums[pos] > 0;
pos = (pos + nums[pos] + len) % len;
if (pos == op) {
break;
}
nums[op] = 0;
}
if (ret) return ret;
}

return ret;
}

``````

• @MyPremiumAccount yeah even [1,2,5,-1,-2] returns false but there is a loop from 5 it jumps to 5 and so on..

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.