InferenceAI

Enter at least two characters to search.

Start free

Pair Sum in Sorted List

easyTwo Pointers15 min10 XP

nums is sorted ascending. Return the 1-based indices [i, j] (i<ji < j) of the first pair found by the two-pointer method that sums to target, or [] if none exists.

Requirements

  • O(n)O(n) time, O(1)O(1) space.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[2,7,11,15],9]
TASKsorted_pair_sum()
EXPECTED OUTPUT[1,2]

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [2,7,11,15], target = 9

Output: [1,2]

Explanation: classic

Input: nums = [2,3,4], target = 6

Output: [1,3]

Explanation: outer pair

Input: nums = [-1,0], target = -1

Output: [1,2]

Explanation: negatives

Hint 1

Start with pointers at both ends; move the left one up if the sum is too small, the right one down if too large.

Topics

DSATwo PointersPython