Pairs with difference k
Submissions: 5708   Accuracy:

28.59%

  Difficulty: Easy   Marks: 2

Given an array A of N positive integers. Find the number of pairs of integers whose difference is equal to a given number K.
Note: (a, b) and (b, a) are considered same. Also, same numbers at different indices are considered different.

 

Input: 
The first line of the input contains T denoting the total number of testcases. Then follows the description of testcases. The first line of each testcase contains two space separated positive integers N and K denoting the size of array and the value of K. The second line contains N space separated positive integers denoting the elements of array.

Output:
Output the number of pairs having difference equal to K in a separate line.

Constraints:
1 <= T <= 100
1 <= N <= 100
1 <= K <= 100
1 <= Ai <= 1000

Example:
Input:
2
5 2
6 4 5 8 7
4 2
1 3 1 8
Output:
3
2

Explanation:
Testcase 1: (6, 4), (5, 7) and (6, 8) are pairs with difference 2.

** For More Input/Output Examples Use 'Expected Output' option **

Author: atharv


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.