backbenchcode
← all problemsnext: Valid Bracket Sequence
#01EasyFoundations · 71% acceptance

Two-Pointer Sum

ArrayTwo PointersHash Map

You are handed an array of integer measurements `nums` and a target value `target`.

Return the indices of the two measurements that add up to `target`. You may assume exactly one valid answer exists, and you cannot use the same reading twice.

If you have ever scanned a gel for two bands that sum to a known weight, you already understand the shape of this problem — we are just making it formal.

Examples

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

Output: [0, 1]

Note: nums[0] + nums[1] = 2 + 7 = 9.

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

Output: [1, 2]

Constraints

Hints

solution.pyPython 3
1
2
3
4
5
6
7
8
9
Test console

// hit Run to execute the sample tests

// Interactive editor preview. Live code execution arrives with your early-access cohort.