BackeasyTwo Pointers

Two Sum II - Sorted Array Solution

Problem Statement

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 their 1-based indices as space-separated integers.

Example 1
Input
4 9 2 7 11 15
Output
1 2

Explanation: 2 + 7 = 9. Index 1 and index 2 (1-based).

Constraints

  • 2 <= numbers.length <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order.
Live Compiler
Sign InSign Up
Loading...
Test Cases & Output
Click "Run" to execute