Skip to main content

21. Two Sum II - Input Array Is Sorted

easyAsked at Vercel

Given a sorted array and a target, return the two indices whose values sum to the target. Vercel asks this to confirm you recognize 'sorted' as the cue for two-pointer over hash map — same instinct you need when their edge data is pre-sorted by request timestamp.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2026-Q1)Vercel screen contrast question after LC 1.
  • LeetCode Discuss (2025-12)Listed in Vercel interview prep notes.

Problem

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.

Constraints

  • 2 <= numbers.length <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order.
  • There is exactly one solution.
  • You may not use the same element twice.

Examples

Example 1

Input
numbers = [2,7,11,15], target = 9
Output
[1,2]

Example 2

Input
numbers = [2,3,4], target = 6
Output
[1,3]

Approaches

1. Hash map (forgetting it's sorted)

Use the LC 1 hash map solution — works but uses O(n) space.

Time
O(n)
Space
O(n)
function twoSum(numbers, target) {
  const seen = new Map();
  for (let i = 0; i < numbers.length; i++) {
    const need = target - numbers[i];
    if (seen.has(need)) return [seen.get(need) + 1, i + 1];
    seen.set(numbers[i], i);
  }
}

Tradeoff: Wastes the sorted property. Vercel specifically asks the sorted variant to see if you'll use two-pointer.

2. Two-pointer from both ends (optimal)

Pointers at start and end. If sum > target, decrement right; if sum < target, increment left; else return.

Time
O(n)
Space
O(1)
function twoSum(numbers, target) {
  let l = 0, r = numbers.length - 1;
  while (l < r) {
    const sum = numbers[l] + numbers[r];
    if (sum === target) return [l + 1, r + 1];
    if (sum < target) l++;
    else r--;
  }
}

Tradeoff: O(1) extra space. The two-pointer invariant: shrinking the window can only happen one of two ways, both correct because the array is sorted.

Vercel-specific tips

Vercel grades whether you reach for two-pointer on the SECOND two-sum question. Bonus signal: noting that two-pointer generalizes to 3sum (LC 15) by fixing one element and two-pointering the rest. Also flag the 1-indexed off-by-one — don't forget the `+ 1`.

Common mistakes

  • Returning 0-indexed — the problem is 1-indexed; LeetCode marks wrong.
  • Using LC 1's hash map — wastes the sorted property.
  • Off-by-one on the while loop: l < r vs l <= r — equality means same element twice, which is forbidden.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • 3Sum (LC 15) — fix one element, two-pointer the rest.
  • 4Sum (LC 18) — two nested fixes plus two-pointer.
  • Two Sum with a custom comparator (e.g., abs distance).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why two-pointer instead of binary search?

Binary search finds one value; two-pointer simultaneously narrows from both sides, exploiting the sortedness to move whichever side is wrong. O(n) total vs O(n log n) for binary-search-per-element.

What if multiple pairs sum to target?

The problem guarantees one. For the multi-pair variant, collect all matches by advancing both pointers when a match is found.

Practice these live with InterviewChamp.AI

Drill Two Sum II - Input Array Is Sorted and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →