const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Two Sum II LeetCode Solution: Sorted Array Walkthrough

Two Sum II (#167) is the canonical opposite-direction two-pointer problem — when the array is sorted, you trade the hash map for two converging pointers and O(1) space.

7 min read|

Sorted input means two pointers, not a hash map

Squeeze from both ends — O(n) time, O(1) space, one clean pass

Two Sum II: Sorted Input Changes Everything

Two Sum II (#167) is the problem that proves sorted input changes everything. If you solved the original Two Sum (#1) with a hash map, you might reach for the same tool here. But when the input array is already sorted, you have a faster path — two pointers that converge from opposite ends with O(1) space.

The problem statement is straightforward: given a 1-indexed sorted array and a target, find two numbers that add up to the target. Return their 1-indexed positions. There is exactly one solution, and you cannot use the same element twice.

This is the canonical introduction to the opposite-direction two-pointer pattern. Once you understand why it works on this problem, you will recognize the same structure in 3Sum, Container With Most Water, and dozens of other sorted-pair problems. This two sum ii leetcode walkthrough covers the approach, the proof, and the edge cases.

Understanding the Two Sum II Problem

You are given an integer array numbers that is sorted in non-decreasing order. Find two numbers such that they add up to a specific target number. Return the 1-indexed positions of the two numbers as an array [index1, index2] where index1 < index2.

The key constraints are: the array is sorted, there is exactly one solution, and you must use constant extra space. The sorted guarantee is what makes the two-pointer approach possible — and the constant space requirement is what makes it necessary.

This is a Medium-difficulty problem on LeetCode, but its real value is pedagogical. Two Sum II (#167) is the sorted variant of the original Two Sum (#1), and it teaches that "sorted input" is a trigger for two-pointer solutions — a pattern recognition skill that interviewers love to test.

Two Pointer Approach — Sorted Two Sum in O(1) Space

Place one pointer at the beginning (left = 0) and one at the end (right = length - 1). Compute the sum of the elements at both pointers. If the sum equals the target, you have found the answer. If the sum is too large, move the right pointer left to decrease it. If the sum is too small, move the left pointer right to increase it.

This opposite two pointer pair technique works because the array is sorted. Moving left forward always increases the smaller value in the pair, raising the sum. Moving right backward always decreases the larger value, lowering the sum. You never need to revisit a pointer position — each step eliminates at least one candidate, so the loop runs at most n times.

The result is O(n) time and O(1) space. No hash map, no extra data structure. Just two index variables and a while loop. This is the two pointer sum pattern at its most elegant.

  1. 1Initialize left = 0 and right = numbers.length - 1
  2. 2While left < right, compute sum = numbers[left] + numbers[right]
  3. 3If sum equals target, return [left + 1, right + 1] (1-indexed)
  4. 4If sum > target, decrement right (shrink the sum)
  5. 5If sum < target, increment left (grow the sum)
💡

Hash Map vs. Two Pointers

The two-pointer approach replaces the hash map when input is sorted — O(1) space instead of O(n). Always check if the input is sorted before defaulting to a hash map.

Why the Two Pointer Approach Works

The correctness of this approach rests on sorted order. At any moment, the left pointer points to the smallest unused candidate and the right pointer points to the largest. If their sum is too big, no other element paired with the right pointer can produce a smaller sum — everything to the left of the right pointer is smaller or equal to the left element. So you safely discard the right pointer position.

The same logic applies in reverse. If the sum is too small, no other element paired with the left pointer can produce a larger sum without moving left forward. Each pointer move eliminates an entire row or column of the implicit pair matrix, which is why the algorithm is complete — it will find the solution if one exists.

Think of it as binary search in two dimensions. In standard binary search you halve the search space each step. Here, each pointer move eliminates one row or one column from the candidate matrix. With n rows and n columns, you need at most n steps to converge.

Visual Walkthrough — Two Sum II Explained

Walk through two examples to see the pattern in action. In each case, left starts at index 0 and right starts at the last index. The pointers squeeze inward until they find the target sum.

Example 1: numbers = [2, 7, 11, 15], target = 9. Left = 0, right = 3. Sum = 2 + 15 = 17, too big — move right to index 2. Sum = 2 + 11 = 13, still too big — move right to index 1. Sum = 2 + 7 = 9, match. Return [1, 2].

Example 2: numbers = [2, 3, 4], target = 6. Left = 0, right = 2. Sum = 2 + 4 = 6, match on the first check. Return [1, 3]. When the answer involves the first and last elements, the algorithm finds it immediately.

Example 3: numbers = [-1, 0], target = -1. Left = 0, right = 1. Sum = -1 + 0 = -1, match. Return [1, 2]. The algorithm handles negative numbers and two-element arrays without any special casing.

  • [2, 7, 11, 15] target 9 -> three steps, answer [1, 2]
  • [2, 3, 4] target 6 -> one step, answer [1, 3]
  • [-1, 0] target -1 -> one step, answer [1, 2]
ℹ️

Pattern Trigger

Two Sum II (#167) is the sorted variant of the original Two Sum (#1) — it teaches that 'sorted input' is a trigger for two-pointer solutions, a pattern recognition skill interviewers love.

Edge Cases for LeetCode 167 Solution

The most common edge cases interviewers probe: the answer uses the first and last elements, the answer uses adjacent elements, the array contains negative numbers, and the array has only two elements (which is guaranteed to be the answer).

A subtle trap is the 1-indexed return format. You solve the problem correctly with 0-based pointers but forget to add 1 before returning. This trips up candidates who solve the logic perfectly but return wrong indices. Always double-check the indexing convention in the problem statement.

Another edge case to consider: duplicate values. If the array is [1, 1, 3, 4] and target is 2, both 1s are valid. The two-pointer approach handles this naturally — left points to the first 1 and right will eventually reach the second 1.

What Two Sum II Teaches — Foundation for 3Sum and Beyond

Two Sum II is not just another LeetCode problem — it is the entry point for an entire family of sorted-pair algorithms. Once you master the opposite two pointer pair pattern here, you can apply it to 3Sum (#15) by fixing one element and running Two Sum II on the remainder. Container With Most Water (#11) uses the same squeeze-from-both-ends logic with a different objective function.

The deeper lesson is pattern recognition: when you see "sorted array" and "find a pair," your first instinct should be two pointers, not a hash map. The hash map approach still works in O(n) time, but it uses O(n) space and misses the signal the interviewer planted by sorting the input.

Review this problem with YeetCode flashcards to lock in the pattern. The two-pointer template for sorted pair finding is one of the highest-frequency interview patterns, and Two Sum II is the cleanest example of it in action.

⚠️

1-Indexed Trap

Return 1-indexed results, not 0-indexed — Two Sum II uses 1-based indexing. This trips up candidates who solve the problem correctly but return wrong indices.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now