Basic example
Input
nums = [1, 5, 3, 4, 2], k = 2
Output
3
Pairs with difference 2: (1,3), (3,5), (2,4).
Full lesson preview
Count how many index-pairs in an array have an absolute difference equal to k.
Problem statement
Task
Examples
Input
nums = [1, 5, 3, 4, 2], k = 2
Output
3
Pairs with difference 2: (1,3), (3,5), (2,4).
Input format
Output format
Constraints
Samples
Input
nums = [1,1,1,2,2], k = 0
Output
4
Three 1s -> 3 pairs, two 2s -> 1 pair, total 4.