Count the number of index pairs whose values differ by exactly k.
Given an integer array nums and an integer k, count how many index pairs (i, j) satisfy:
i < j|nums[i] - nums[j]| = kYour task is to return the total number of such pairs.
A pair is counted by index, so two equal values at different positions can contribute multiple pairs.
numskThe exact platform input format may vary, but the core task is to count pairs of indices with absolute difference k.
Return an integer: the number of index pairs (i, j) such that i < j and |nums[i] - nums[j]| = k.
0 <= kk = 0, only equal-value pairs contribute.Example 1
Input
nums = [1, 2, 2, 1], k = 1
Output
4
Explanation
Valid pairs are (1st 1, first 2), (1st 1, second 2), (second 1, first 2), and (second 1, second 2).
Example 2
Input
nums = [1, 3, 5, 8], k = 2
Output
2
Explanation
The valid pairs are (1, 3) and (3, 5).
Example 3
Input
nums = [1, 1, 1, 1], k = 0
Output
6
Explanation
Every pair of equal values counts, so the answer is C(4, 2) = 6.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.