Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

The Converging Pointer Pattern: Efficient Pair Searching in Arrays

Imagine you’re at a bustling farmers’ market, and you want to pick two fruits whose combined price exactly matches your budget. You glance at the price tags from both ends of the row and adjust your picks until you hit the perfect match. Surprisingly, this simple intuition is exactly what the converging two-pointer approach does in arrays.

If you’ve read our previous post on the two-pointer pattern, you know it involves using two pointers to explore an array efficiently.Today, we dive deeper into a specific subpattern: target sum using converging pointers.

Converging Pointer Pattern

The Converging Pointer Pattern involves two pointers starting at opposite ends of a sorted array. The pointers move toward each other to efficiently find a condition — often a target sum or pair meeting some requirement.

Problem

Given a sorted array of numbers, find whether any two numbers sum up to a target value.

For instance, in the array [1, 2, 4, 6, 8], can you find two numbers that add up to 10?

Core Idea

Move two pointers through the array in a way that each element is considered at most once. Compare the sum of the elements at the two pointers with the target:

  • If the sum is less than the target, move the left pointer right to increase the sum.
  • If the sum is greater than the target, move the right pointer left to decrease the sum.
  • Stop when the pointers meet or when you find a pair that matches the target.
  • This approach ensures we don’t check the same pair twice and find the result efficiently.

    Think of it like two explorers walking from opposite ends of a bridge, meeting in the middle once they’ve found the right gap.
    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/convergence_two_pointer.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260117%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260117T151240Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=7d1f425d03490f5e87da4771592678f132c856e4901881ba831878e343913b7ee79b6d6e07b0f44c8ea2f9e84f4753dc2bbf9a8cc2f31d404030c0cd2a268d2ad6526a4445661dbc56715db8afc2acfa1f7114496847525bd53ad6924efeb10717ffeedaeda8109c79bd241a57080f4e99c9a6edf999fd3a211d3b33069f492dd12ada6c546c56229fa7468829261deb9784a0ee088fb6178be433982fa033a93defd3e923f8411dc78d1727903e93a83dc0affb788d482caadc20863cd6777d8f7af8be3b4dfbd3e5e19d79f929019dfef5ddb1083bcc943d0a8393472ba00d74a9461417337b2967a256a176f6b330769a9f5b2cc1790a99aeca152faf00fa

    step-by-step flow

    Start
      |
      v
    Initialize left = 0 (points to 1), right = 4 (points to 8)
      |
      v
    Step 1: total = input[left] + input[right] = 1 + 8 = 9
      |
      +-- Is total == target (10)? --> No
      +-- Is total < target? --> Yes --> move left pointer right (left++)
      |
      v
    Step 2: left = 1 (points to 2), right = 4 (points to 8)
      |
      v
    total = 2 + 8 = 10
      |
      +-- Is total == target? --> Yes --> return true
      |
      v
    End: target sum found (2 + 8 = 10)

    Solution

    fun targetSumConvergencePointer(input: IntArray, target: Int): Boolean {
        if (input.size < 2) return false
        var left = 0
        var right = input.size - 1
        while (left < right) {
            val total = input[left] + input[right]
            when {
                total == target -> {
                    return true
                }
                total > target -> {
                    right--
                }
                else -> {
                    left++
                }
            }
        }
        return false
    }

    Time Complexity

    O(n) – We go through the array just once, with the two pointers moving toward each other.

  • Each element is looked at at most one time.
  • No extra loops are involved, so the time grows linearly with the size of the array.
  • Space Complexity

  • O(1) – We only need two variables (left and right) to keep track of the pointers.
  • No extra arrays or data structures are created.
  • The memory we use stays the same, no matter how big the array is.
  • Conclusion

    The converging two-pointer approach shows how a simple idea—starting two pointers at opposite ends and moving them based on a condition—can solve problems like finding a pair with a target sum efficiently.

  • Fast: Only one pass through the array.
  • Memory-efficient: Uses just two pointer variables.
  • Intuitive: Easy to visualize and reason about, especially with sorted data.
  • With this pattern, you learn to think in terms of adjusting your exploration based on the current state, which is a skill that extends to many other array and sequence problems.

    Once you’ve mastered converging pointers, the next trick is the sliding window. Imagine a flexible window moving across the array or string, expanding or shrinking to capture exactly what you need. In the next post, we’ll dive deeper into this pattern—until then, keep practicing and happy coding!

    #problem_solving#data_structures#time_complexity#array_algorithms#algorithm_intuition#two_pointer_pattern#converging_pointers#target_sum_problem#dsa_patterns#kotlin_dsa#linear_time_algorithm#efficient_search#coding_patterns#space_optimized#interviewer_patterns