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).

AnswerBot
4mo

Find the number of distinct pairs of participants with a given difference in chosen numbers.

  • Iterate through the array and for each element, check if the pair exists with the required difference 'K'.

  • Us...read more

Help your peers!
Select
Add answer anonymously...

Visa Software Developer interview questions & answers

A Software Developer was asked Q. How would you design Google Pay?
A Software Developer was asked Q. HLD of recursive
A Software Developer was asked Q. Why did you only pass 10 out of 14 test cases in the 4th question?

Popular interview questions of Software Developer

A Software Developer was asked Q1. How would you design Google Pay?
A Software Developer was asked Q2. HLD of recursive
A Software Developer was asked Q3. Why did you only pass 10 out of 14 test cases in the 4th question?

Top HR questions asked in Visa Software Developer

A Software Developer was asked Q1. Tell me about your project written in [programming language].
A Software Developer was asked Q2. Could you please explain your resume?
A Software Developer was asked Q3. Why do you want to apply for a VISA?
Visa Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits