#K59202. Count Pairs with Given Difference
Count Pairs with Given Difference
Count Pairs with Given Difference
You are given an array of n integers and an integer k. Your task is to determine the number of pairs of indices (i, j) (with i < j
) such that the absolute difference between the corresponding elements is exactly k, i.e. \(|arr[i] - arr[j]| = k\).
Example:
Input: 5 2 1 5 3 4 2</p>Output: 3
In the above example, the pairs are (1, 3), (5, 3), and (4, 2) resulting in a total count of 3.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains two integers n and k, where n is the number of elements in the array and k is the target absolute difference.
- The second line contains n integers separated by spaces representing the array elements.
outputFormat
Output to standard output (stdout) a single integer that represents the number of pairs in the array that have an absolute difference equal to k.
## sample5 2
1 5 3 4 2
3