Ad
  • Custom User Avatar

    Definitely agree. Can terminate after 5 matches.

  • Custom User Avatar

    You don't actually need to even traverse the entire list in most scenarios.
    In a best case scenario you can return after just 3 iterations, for example, let's assume the target value (in this case odd) is at index 0 in the array. You record the index of the odd value within an oddIndexes list. You then make a further iteration and find an even value which, the index of which you store in an evenIndex list. You then make one more iteration and find another even value, at this point you now know the target value has to be odd of which you already have the index stored.
    Worst case scenario is that the odd value is at the end of the array and you have to iterate the whole way through.