Count Number of Pairs With Absolute Difference K
Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
The value of |x| is defined as:
xifx >= 0.-xifx < 0.
Example 1
Input
nums = [1,2,2,1], k = 1Output
4The pairs with an absolute difference of 1 are the four pairs formed by each
1 with each 2.Example 2
Input
nums = [1,3], k = 3Output
0There are no pairs with an absolute difference of 3.
Constraints
- 1 <= nums.length <= 200
- 1 <= nums[i] <= 100
- 1 <= k <= 99