FunctionRacerProblemsRace

Two Sum II Sorted Input

EasyTwo pointersPython, JavaScript, TypeScript, Java, C++

numbers is sorted in non-decreasing order. Exactly one pair of different positions adds up to target.

Return [i, j], the 1-based positions of that pair, with i < j. Two pointers walking in from both ends work well.

Example 1

numbers [2,7,11,15]
target 9
returns [1,2]

2 + 7 = 9, at positions 1 and 2.

Example 2

numbers [-3,0,4,6]
target 3
returns [1,4]

-3 + 6 = 3, at positions 1 and 4.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems