Keywords and Patterns

Whenever you have to keep track of two things at once, this is probably the go-to. Of course there are n-pointers problems, but in principle, most of them are the same.

An evolution of the two pointers problem would be binary search

The pattern goes as such,

 
# based on the defined search space
# in this case, the entire list
left = 0
right = len(some_list) - 1
 
while left < right:
    if condition_left:
        left += 1
    elif condition_right:
        right -= 1
    else:
        # set of condition
        left += 1
        right -= 1

The condition to move the pointers is the tricky part

Examples

Valid Palindrome

Problem here: Valid Palindrome

This is a relatively short but important topic, so make sure this is understood well.

Problems