I cannot understand why test case [-2, 1, -1, -2, -2] gives false?


  • 29
    C

    For example, starting at index 1, nums[1] is 1, move 1 step forward to index 2. Then nums[2] is -1, move back 1 step to index 1. The loop contains indices 1 and 2. Is this a valid loop?


  • 7
    P

    @czhangaegean This was my question exactly. See the last line in the problem statement. Loop can be either "forward" or "backward", ie. you cannot change direction. So no, 1->2, 2->1 is not a valid loop.


  • 0
    S

    they said loop is either forward OR backward, including negative numbers in array is a valid test case but including negative number in a loop is invalid
    so the answer is false as this is forward loop


  • 0
    M

    It is a good question. At least, many people will interpret the loop the same way as what @czhangaegean thinks about.
    The forward loop or backward one stated at the end of the problem is not a common way to interpret loop.


  • 1
    Q

    @satyaaditya Then what about the case [-1,2]? If we start from index 1 and search forwards: because nums[1]=2 so it's 1->1. So I think it's also a valid answer, isn't it?


  • 1
    B

    @qintxx A loop starts and ends at a particular index with more than 1 element along the loop.


  • 0
    I

    @princessmaja said in I cannot understand why test case [-2, 1, -1, -2, -2] gives false?:

    @czhangaegean This was my question exactly. See the last line in the problem statement. Loop can be either "forward" or "backward", ie. you cannot change direction. So no, 1->2, 2->1 is not a valid loop.
    @princessmaja : Can you please explain what is the meaning of forward and backward loop here ? Considering the given example why this [0->3->1->2->1] is not considered to be a loop ?


  • 0

    This is a good task, good question. But again, bad description.

    Why is it so hard for them to put some more words in the description and make it clearer?


  • 0
    K

    Because the question says that there needs to be more than one element along the loop.


  • 0
    G

    I ran in the same question, yet I would say the problem is well worded... and cunning. :)
    The forward and backward loops could have been defined explicitly yet the reader can figure out what they mean... once he/she pays attention to them! In the heat of the fight I certainly did not.
    In case of the [-1,2] observe that there is no valid loop. 2 maps to itself and 1 maps to 2.
    In case of [1,1] we have a valid loop a both "edges" go forward and close a loop of 2 vertices.
    Finally, see that [1,-1] does not contain a valid loop as one of the edges goes forward and the other one goes backward.

    Hope that helps. Note that If you have solved the problem for finding any loop of at least two vertices, chances are your code can be quickly modified to the right one.


Log in to reply
 

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