The sequence of loop goes like 1->4->3->2->1;
but the input [1, 2, 3, 4, 5] has loop 0->1->3->2->0, it returns true.
In both cases, the index goes far and then back, why the answer is different?
To put it simple, "forward" indirectly means all elements in the loop have to be positive and the same thing goes for "backward". This is because "forward" or "backward" is defined by the sign of the element. In your first example, 1->4 is a forward step, 4->3 is a backward step so it returns false. In second example, however, all of the steps are forward, including the one from 5 to 1, so it returns true
@jianxian2 Thanks for the explanation! It is quite clear.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.