Skip to content
Ultimate Algorithm

Collision Pointers

Collision Pointers The two pointers start at two different points, usually the two ends, and move towards each other. This is used in problems like “two sum”, where you need to find two numbers in a sorted array that add up to a target value. This technique is often used on sorted arrays or linked lists.

def two_sum_sorted_array(arr, target):
    left = 0
    right = len(arr) - 1

    while left < right:
        current_sum = arr[left] + arr[right]

        if current_sum == target:
            return (left, right)  # or return the elements: return (arr[left], arr[right])
        elif current_sum < target:
            left += 1  # Move right to increase the sum
        else:
            right -= 1  # Move left to decrease the sum

    return None  # No pair was found
ProblemTopicPlatform
Two Sum IITwo-PointersLeetcode
3SumTwo-PointersLeetcode
3Sum ClosestTwo-PointersLeetcode
4SumTwo-PointersLeetcode
Valid Triangle NumberTwo-PointersLeetcode
Trapping Rain WaterTwo-PointersLeetcode
Container With Most WaterTwo-PointersLeetcode
Partition ArrayTwo-PointersLeetcode
Valid PalindromeTwo-PointersLeetcode
Sort ColorsTwo-PointersLeetcode
Parition Array by Odd and EvenTwo-PointersLeetcode
Sort Letters by CaseTwo-PointersLeetcode