Ninja's Dance Competition Pairing Problem

Ninja is organizing a dance competition and wants to pair participants using a unique method. Each participant chooses a number upon entry, and Ninja selects a number ‘K’. Participants are paired if the absolute difference between their chosen numbers is equal to ‘K’.

Your task

Help Ninja find the number of distinct pairs of participants whose numbers have differences equal to ‘K’.

Input:

The first line contains a single integer ‘T’ representing the number of test cases. The first line of each test case contains two space-separated integers ‘N’ and ‘K’, where ‘N’ is the number of elements and ‘K’ is the required difference between the paired numbers. The second line contains ‘N’ space-separated integers denoting the numbers chosen by participants.

Output:

For each test case, print a single integer denoting the number of distinct pairs with differences equal to ‘K’. The output for each test case should be on a separate line.

Example:

Let us suppose the numbers chosen by participants are: [2, 6, 5, 2, 3] and K = 3. The distinct pairs having differences equal to K are: [2, 5] and [3, 6]. Hence, the output should be 2.

Constraints:

  • 1 <= T <= 10^2
  • 0 <= N <= 10^4
  • 0 <= K <= 10^4
  • 0 <= ARR[i] <= 10^9
Note:

The list of numbers can contain duplicates, but only distinct pairs should be considered in the output. For instance, for [2, 2, 3, 4] and K = 1, the output should be 2, corresponding to the pairs (2, 3) and (3, 4).

Be the first one to answer
Add answer anonymously...
Visa Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter