Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

24. Two Sum II: Input Array Is Sorted

MediumDay 7 of 60Two Pointers

You are given a 1-indexed array numbers sorted in non-decreasing order and a target. Find two different positions whose values add up to target, and return them as [index1, index2] with 1 <= index1 < index2. Exactly one solution exists, and you must use only constant extra space.

Example 1
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
2 + 7 = 9, at 1-indexed positions 1 and 2.
Example 2
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Constraints
  • 2 <= numbers.length <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order
  • -1000 <= target <= 1000
  • Exactly one answer exists
Hints
Frequently asked at: Amazon, Adobe, Apple, Zoho
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min