Two Sum II – Input Array Is Sorted
Problem Statement
You are given an array numbers that is already sorted from smallest to largest, and a number target.
Find two different positions whose values add up to exactly target. Return those two positions as [index1, index2], where positions are counted from 1 (not 0) and index1 < index2.
There is always exactly one such pair. You may not use the same position twice, and your solution should use only constant extra space.
A sorted integer array numbers and an integer target.
An array of two 1-based positions [index1, index2] with numbers[index1] + numbers[index2] == target.
2 + 7 = 9. They are at positions 1 and 2 (counting from 1).
2 + 4 = 6. Note that 3 + 3 is not allowed — that would use position 2 twice.
−1 + 0 = −1.
- • 2 ≤ numbers.length ≤ 3 · 10^4
- • −1000 ≤ numbers[i] ≤ 1000
- • numbers is sorted in non-decreasing order
- • −1000 ≤ target ≤ 1000
- • Exactly one valid pair exists.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check every pair (i, j) with i < j
For each number, binary search the rest for target − number
Too small → move left right; too big → move right left
Try every pair of positions i < j. The first pair whose sum equals target is the answer.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.
Circular Buffer & Ring Queue
Two pointer read/write head synchronization in production memory pools.
Pointers vs References Deep Dive
Pointer arithmetic, dereferencing semantics, and memory layout behind in-place array passes.
const Correctness & East-Const
Passing read-only spans with const int* without copying buffers.