Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Two Pointers
Google
Two Sum II - Input Array Is Sorted

Find two numbers in a sorted array whose sum equals a target, and return their 1-based positions.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.

Two Sum II

gfg
Primary
Problem Statement

Problem

You are given a 1-indexed sorted integer array numbers in non-decreasing order and an integer target.

Find two distinct elements in numbers such that their sum is exactly target.

Return the indices of the two numbers as a pair [index1, index2], where index1 < index2.

It is guaranteed that exactly one valid pair exists.

Notes

  • Use the array as provided; do not sort it again.
  • The answer must use 1-based indexing.
  • Because the array is sorted, there is a more efficient approach than checking every pair.

Input Format

  • numbers: a sorted integer array in non-decreasing order
  • target: an integer

The function should return two indices i and j such that numbers[i] + numbers[j] = target and i < j (using 1-based indexing).

Output Format

Return an array of two integers: [index1, index2].

Constraints

  • 2 <= numbers.length
  • numbers is sorted in non-decreasing order
  • There is exactly one solution
  • 1 <= index1 < index2 <= numbers.length
Examples
Sample cases returned by the problem API.

Example 1

Input

numbers = [2,7,11,15], target = 9

Output

[1,2]

Explanation

The first two numbers add up to 9, so the 1-based indices are returned.

Example 2

Input

numbers = [2,3,4], target = 6

Output

[1,3]

Explanation

2 + 4 = 6, so the correct answer is the indices of those values.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.