#C7962. Count Unique Pairs with Difference K
Count Unique Pairs with Difference K
Count Unique Pairs with Difference K
You are given an array of integers and an integer k. Your task is to count the number of unique pairs \( (i, j) \) such that the absolute difference between the numbers is equal to k, i.e., \( |nums[i] - nums[j]| = k \). A pair is considered unique if the two numbers in the pair are distinct (i.e., each pair is counted only once regardless of order).
Note: If k is negative, the answer is automatically 0.
For example:
- For the array [1, 5, 3, 4, 2] and k = 3, the unique pairs are (1,4) and (2,5), so the answer is 2.
- For the array [1, 1, 1, 1] and k = 0, there is only one unique pair (1,1), so the answer is 1.
This is a typical problem that tests your understanding of array manipulation and set operations.
inputFormat
The input is read from standard input (stdin) and consists of three lines:
- The first line contains an integer n, the number of elements in the array.
- The second line contains n space-separated integers representing the elements of the array.
- The third line contains an integer k.
outputFormat
Print a single integer to standard output (stdout) which is the count of unique pairs that satisfy \( |nums[i] - nums[j]| = k \).
## sample5
1 5 3 4 2
3
2