#K6271. Count Unique Pairs with Given Difference
Count Unique Pairs with Given Difference
Count Unique Pairs with Given Difference
You are given an array of integers and an integer ( k ). Your task is to determine the number of unique pairs ( (a, b) ) such that ( b - a = k ).
If ( k > 0 ), a pair ( (a, b) ) is counted only once even if multiple occurrences exist. For ( k = 0 ), count each distinct number that appears at least twice (each such number contributes exactly one pair). For ( k < 0 ), the answer is defined to be 0.
Please read the input from standard input (stdin) and output the result to standard output (stdout).
inputFormat
The first line of input contains two integers ( n ) and ( k ), where ( n ) is the number of elements in the array and ( k ) is the difference.
The second line contains ( n ) space-separated integers representing the array.
outputFormat
Output a single integer representing the number of unique pairs that satisfy the condition.## sample
7 2
1 7 5 9 2 12 3
4