FunctionRacerProblemsRace

Pairs With Difference K

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

Count the pairs of positions (i, j) of the list nums with i < j such that the absolute difference abs(nums[i] - nums[j]) equals the number k.

Example 1

nums [1,2,2,1]
k 1
returns 4

Each 1 pairs with each 2: 4 pairs.

Example 2

nums [3,2,1,5,4]
k 2
returns 3

Pairs (3,1), (3,5), (2,4): 3.

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