CodePark/ problems / Pair Sum in a Sorted List
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Two pointers

Pair Sum in a Sorted List

easyabout 12 minutes in an interview

You're given an array nums and a number target. nums is sorted in non-decreasing order: each number is greater than or equal to the one before it. Find two different positions i < j where nums[i] + nums[j] === target.

  • Return the 0-based indices as [i, j].
  • At most one pair of positions works. If no pair works, return [].
  • nums can contain negative numbers and duplicates. It can have up to 100,000 elements.
  • Use O(1) extra memory, which means a fixed amount that doesn't grow with nums. So don't use a map or a set.
Input: nums = [1, 2, 4, 7, 11, 15], target = 15 Output: [2, 4]nums[2] + nums[4] = 4 + 11 = 15
Input: nums = [-6, -1, 0, 3, 8], target = 2 Output: [0, 4]
Input: nums = [1, 3, 5], target = 7 Output: []