Two Sum II

Given a sorted array of integers and a target, return the indices of two distinct elements whose sum equals the target. The sorted order allows a classic two-pointer scan from both ends.

Input Format

nums = sorted array of integers, target = required sum

Output Format

0-based indices of the pair

Constraints

  • 2 <= nums.length <= 10^5; -10^9 <= nums[i], target <= 10^9; nums is sorted in non-decreasing order

Examples

Example 1:

Input:

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

Output:

[1,2]

Explanation:

2 + 7 = 9.

Example 2:

Input:

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

Output:

[1,3]

Explanation:

2 + 4 = 6.

Loading...
Two Sum II - Sorting Based Array Problems