Two Sum II Input Array Is Sorted

Asked byAmazonAdobeGoogleAppleMicrosoftYahoo

Problem

Given a 1-indexed array of integers already sorted in non-decreasing order, find two numbers that add up to a target value and return their indices (1-indexed), using only O(1) extra space.

Examples

Example 1
Input:numbers = [2,7,11,15], target = 9
Output:[1,2]
numbers[0] + numbers[1] = 2 + 7 = 9.
Example 2
Input:numbers = [2,3,4], target = 6
Output:[1,3]

Constraints

  • 2 <= numbers.length <= 3 * 10^4
  • Exactly one solution exists.
  • You may not use the same element twice.

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Two Pointers pattern

Two indices move through a sorted (or sortable) structure — often from opposite ends or at different speeds — to avoid the nested loops a brute-force scan would need.

Look for this pattern when

  • The input is sorted, or can be sorted without losing information you need.
  • You're looking for a pair, triplet, or partition that satisfies a sum or comparison condition.
  • You need to compare values from both ends of a sequence (e.g. palindrome checks).

Read the full Two Pointers guide →

Video walkthroughs

Original problem on LeetCode ↗

More Two Pointers problems