#C9607. Maximum Operations with Pair Differences
Maximum Operations with Pair Differences
Maximum Operations with Pair Differences
You are given an integer array A
of length N
and an integer K
. In one operation, you can choose a pair of numbers from the array whose absolute difference is exactly \(K\) and remove both numbers from the array. Each element may be used at most once. Your task is to determine the maximum number of operations you can perform on the array.
Note: If K = 0
, then a valid pair consists of two identical numbers. Otherwise, for any K > 0
, you should pair an element x
with an element x + K
if possible. Make sure that each element is used at most once.
Example:
- For
N = 5
,K = 3
andA = [1, 4, 7, 10, 13]
, the answer is2
since you can remove the pairs(1, 4)
and(7, 10)
.
inputFormat
The first line contains an integer T
, the number of test cases. Each test case consists of two lines:
- The first line contains two integers
N
andK
. - The second line contains
N
space-separated integers representing the arrayA
.
outputFormat
For each test case, output a single integer representing the maximum number of operations that can be performed, each on a new line.
## sample5
5 3
1 4 7 10 13
6 2
2 4 6 8 10 12
4 10
1 2 1 2
6 1
1 2 2 3 3 4
1 1
1
2
3
0
3
0
</p>