Why is the time complexity of searchPair O(N)?
searchPair contains an outer loop, and, potentially two inner while loops.
Inners loop I am referring to are:
while (left < right && arr[left] == arr[left - 1])
left++; // skip same element to avoid duplicate triplets
while (left < right && arr[right] == arr[right + 1])
right--; // skip same element to avoid duplicate triplets
For a scenario like [-5,2,2,2,2,2,3,3,3,3,3,3], I don’t understand why these inner while loops do not also have a time complexity of O(N)?
Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Triplet Sum to Zero (medium) - Grokking the Coding Interview: Patterns for Coding Questions