Two-Pointer Technique in C

Advanced
8 min read
Updated: Nov 2026
2 examples

Two Indexes, One Pass

The two-pointer technique uses two indexes that move through an array under a rule, replacing a nested loop with a single O(n) pass. Opposite ends: start at both ends of a sorted array and move inward based on a sum. Same direction (slow/fast): a read pointer scans ahead while a write pointer builds the result in place.

Remember
lo = 0; hi = n - 1;
while (lo < hi) {
    s = a[lo] + a[hi];
    if (s == target) found;
    else if (s < target) lo++;
    else hi--;
}

Cheat Sheet

Keep these facts handy while you work with Two-Pointer Technique in C in C.

TopicDetail
Opposite endsSorted pair sums
Slow and fastIn-place filtering
TimeO(n)
RequiresOften sorted input

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Sliding Window Technique in C.

Example 1 — Pair with a target sum in a sorted array

Target 15 in 1, 2, 4, 7, 11, 15.

C
#include <stdio.h>

int main(void) {
    int a[] = {1, 2, 4, 7, 11, 15};
    int lo = 0, hi = 5, target = 15, steps = 0;

    while (lo < hi) {
        steps++;
        int s = a[lo] + a[hi];
        if (s == target) {
            printf("%d + %d = %d (indices %d and %d) after %d steps\n",
                   a[lo], a[hi], target, lo, hi, steps);
            return 0;
        }
        if (s < target) lo++;
        else hi--;
    }
    printf("no pair\n");
    return 0;
}

How It Works

  1. 1 + 15 is too large, so hi moves left.
  2. 1 + 11 and 2 + 11 are too small, so lo moves right twice.
  3. 4 + 11 matches. The nested-loop version would have checked many more pairs.

Example 2 — Remove duplicates in place

A slow pointer writes and a fast pointer reads.

C
#include <stdio.h>

int main(void) {
    int a[] = {1, 1, 2, 2, 2, 3, 4, 4};
    int n = 8, slow = 0;

    for (int fast = 1; fast < n; fast++)
        if (a[fast] != a[slow]) a[++slow] = a[fast];

    printf("unique count: %d\nvalues:", slow + 1);
    for (int i = 0; i <= slow; i++) printf(" %d", a[i]);
    printf("\n");
    return 0;
}

How It Works

  1. fast scans every element once.
  2. Whenever it finds a value different from the last kept value, slow advances and stores it.
  3. The first slow + 1 elements hold the distinct values in order.

Edge Cases & Pitfalls

Check these before relying on Two-Pointer Technique in C in real programs.

unsorted

Unsorted input

The opposite-ends pair search is only correct on sorted arrays.

bounds

Pointers crossing

Use lo < hi so a single element is not paired with itself.

Key Takeaways

  • Replace nested loops.
  • Opposite ends or slow/fast.
  • O(n) on sorted data.

One line: The two-pointer technique uses two indexes that move through an array under a rule, replacing a nested loop with a single O(n) pass.

Frequently Asked Questions

Using two indexes that move through the data according to a rule to solve problems in a single pass instead of with nested loops.
Typically on sorted arrays or sequences where moving a pointer has a predictable effect on the result.
Continue with "Sliding Window Technique in C" next. Compute window sums incrementally with fixed and variable windows. The C Algorithm Techniques hub lists every topic in order.

Next: Sliding Window Technique in C

Compute window sums incrementally with fixed and variable windows.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful