Pairs with Difference less than K
Easy Accuracy: 23.01% Submissions: 3207 Points: 2

Given an array of N integers, the task is to find all pairs with difference less than K.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. First line of each test case contains two integers N & K and the second line contains N space separated array elements.

Output:
For each test case, print the count of required pairs in new line.

Constraints:
1<=T<=100
2<=N<=104
0<=K<=103
1<=A[i]<=105

Example:
Input:

2
4 3
1 10 4 2
3 5

2 3 4

Output:
2
3

Explanation:

Input : a[] = {1, 10, 4, 2}
        K = 3
Output : 2
We can make only two pairs with difference less than 3.
(1, 2) and (4, 2)

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.