Timer is Running

Pairs with difference k
Submissions: 6646   Accuracy:


  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.


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 the number of pairs having difference equal to K in a separate line.

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

5 2
6 4 5 8 7
4 2
1 3 1 8

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.