30:00
Given an integer array nums and an integer k, return the number of pairs (i, j) with i < j such that |nums[i] - nums[j]| == k.
1≤nums.length≤2001 \leq \text{nums.length} \leq 2001≤nums.length≤200
1≤nums[i]≤1001 \leq \text{nums}[i] \leq 1001≤nums[i]≤100
1≤k≤991 \leq k \leq 991≤k≤99
Run code to see results.
Lay the values out on a number line with a tally for each. A new value x pairs with every earlier x - 1 and x + 1, so only those two tallies matter.
nums · k = 1
0
1
2
3
number line · tally of values seen so far
Bars above a value count its earlier copies. Blue is x - k, pink is x + k, amber is the current value.
pairs
Given an integer array nums and an integer k, count the index pairs (i, j) with i < j whose values differ by exactly k in absolute value.