Skip to content
BASA DSA

Two Sum

EasyArrays & Hashing

Return two distinct zero-based indices whose values sum to target. Either index order is allowed.

Example 1

Input
nums = [4,8,2,7]
target = 11
Output
[0,3]

Explanation

4 and 7 at indices 0 and 3 sum to 11.

Example 2

Input
nums = [5,5]
target = 10
Output
[0,1]

Explanation

The two separate occurrences of 5 sum to 10; one index cannot be used twice.

Example 3

Input
nums = [-4,8,1]
target = -3
Output
[0,2]

Explanation

-4 + 1 = -3, selecting indices 0 and 2.

Constraints

  • 2 <= len(nums) <= 10000
  • Exactly one index pair is a solution
  • Values and target are signed 32-bit integers

Function contracttwoSumArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…