FunctionRacerProblemsRace

Pairs That Add Up

EasyHash mapsPython, JavaScript, TypeScript, Java, C++

Count the pairs of positions (i, j) with i < j such that nums[i] + nums[j] == target.

Positions are what is counted: equal values at different positions make different pairs. Return the count, which is 0 when nums has fewer than two items.

Example 1

nums [1,5,3,3,7]
target 6
returns 2

1 + 5 and 3 + 3 (the two 3s at positions 2 and 3) add up to 6: two pairs.

Example 2

nums [2,2,2]
target 4
returns 3

Any two of the three 2s add up to 4: three pairs of positions.

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 Hash maps problems