leet++Two Sum
EasyHash mapArray

Two Sum

Write a function that finds two different positions in an integer list whose values add up to a given target. Return the positions in either order, and assume one valid pair exists.

Examples

Input: values = [8, 3, 12, 5], target = 8

Output: [1, 3]

The values at positions 1 and 3 total 8.

Input: values = [-2, 7, 4], target = 2

Output: [0, 2]

The values at positions 0 and 2 total 2.

Constraints & goals

  • The list contains at least two integers.
  • Do not use the same position twice.
  • Aim for better than checking every pair when possible.

Camera feed

Preview
Camera starts when the session begins
Read-only

Transcribed whiteboard text will appear here.