Bird
0
0
DSA Cprogramming

Two Pointer Technique on Arrays in DSA C

Choose your learning style9 modes available
Mental Model
Use two markers moving through the array to find pairs or conditions without checking every pair.
Analogy: Imagine two friends starting at opposite ends of a hallway, walking towards each other to find a matching pair of shoes quickly.
Array: [1] -> [2] -> [3] -> [4] -> [5] -> null
Pointers: ↑left                             ↑right
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], find if any two numbers sum to 7
Goal: Find two numbers in the array that add up to 7 using two pointers
Step 1: Set left pointer at index 0 (value 1), right pointer at index 4 (value 5)
[1]↑left -> [2] -> [3] -> [4] -> [5]↑right -> null
Why: Start with pointers at both ends to check sums efficiently
Step 2: Calculate sum = 1 + 5 = 6, which is less than 7, move left pointer right
[1] -> [2]↑left -> [3] -> [4] -> [5]↑right -> null
Why: Sum too small, increase sum by moving left pointer forward
Step 3: Calculate sum = 2 + 5 = 7, found target sum, stop
[1] -> [2]↑left -> [3] -> [4] -> [5]↑right -> null
Why: Sum matches target, solution found
Result:
[1] -> [2] -> [3] -> [4] -> [5] -> null
Pair found: 2 + 5 = 7
Annotated Code
DSA C
#include <stdio.h>
#include <stdbool.h>

bool two_pointer_sum(int arr[], int size, int target) {
    int left = 0;
    int right = size - 1;
    while (left < right) {
        int sum = arr[left] + arr[right];
        if (sum == target) {
            printf("Pair found: %d + %d = %d\n", arr[left], arr[right], target);
            return true;
        } else if (sum < target) {
            left++; // increase sum by moving left pointer right
        } else {
            right--; // decrease sum by moving right pointer left
        }
    }
    printf("No pair found that sums to %d\n", target);
    return false;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target = 7;
    two_pointer_sum(arr, size, target);
    return 0;
}
while (left < right) {
loop until pointers meet or cross
int sum = arr[left] + arr[right];
calculate sum of values at pointers
if (sum == target) {
check if sum matches target
left++; // increase sum by moving left pointer right
move left pointer right to increase sum when sum too small
right--; // decrease sum by moving right pointer left
move right pointer left to decrease sum when sum too large
OutputSuccess
Pair found: 2 + 5 = 7
Complexity Analysis
Time: O(n) because each pointer moves at most n steps through the array once
Space: O(1) because only two pointers and a few variables are used
vs Alternative: Compared to checking all pairs (O(n^2)), this is much faster by avoiding repeated checks
Edge Cases
empty array
function returns no pair found immediately
DSA C
while (left < right) {
array with one element
function returns no pair found because pointers cannot cross
DSA C
while (left < right) {
no two numbers sum to target
function prints no pair found and returns false
DSA C
printf("No pair found that sums to %d\n", target);
When to Use This Pattern
When you need to find pairs or conditions involving two elements in a sorted array, use two pointers moving inward to find answers efficiently.
Common Mistakes
Mistake: Not moving the correct pointer based on sum comparison
Fix: If sum is less than target, move left pointer right; if sum is greater, move right pointer left
Summary
Find pairs in a sorted array that meet a condition using two pointers.
Use when searching pairs or conditions in sorted arrays to reduce time.
Move pointers inward based on sum comparison to avoid checking all pairs.