Ninja And The Dance Competetion

Easy
0/40
Average time to solve is 10m
profile
Contributed by
16 upvotes
Asked in companies
MicrosoftVisaSalesforce

Problem statement

Ninja has been asked to organize a dance competition. Ninja decided that he will take individual entries and then will divide them into pairs. As part of the entry, he asked the participants to choose any number.

Ninja then thought of a creative method to divide them into pairs. He decided that he would select a number ‘K’, and then select numbers from the list that have a difference equal to ‘K’.

You need to help Ninja in finding the number of distinct pairs from the numbers with differences equal to ‘K’.

Example:
Let us suppose the numbers are chosen by participants: [2, 6, 5, 2, 3] and K = 3, then the distinct pairs having differences equal to K are: [2, 5] and [3, 6] so print 2. 
Note:
The list of numbers can contain duplicate numbers, you need to print only the distinct pairs.

For example [2, 2, 3, 4] and K = 1, so you need to print 2 as the two distinct pairs are: (2, 3) and (3, 4).
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain two space-separated integers ‘N’ and ‘K’, where ‘N’ denotes the number of elements of the array that contains the chosen numbers, and ‘K’ denotes the difference required between the pair elements.

The second line of each test case will contain ‘N’ space-separated integers denoting the chosen numbers by the participants.
Output Format:
For each test case, print a single integer that denotes the distinct pairs having differences equal to K. 

Output for every test case will be printed in a separate line.
Constraints:
1 <= T <= 10^2
0 <= N <= 10^4
0 <= K <= 10^4
0 <= ARR[i] <= 10^9

Where ‘ARR[i]’ is the value of elements of the array.

Time Limit: 1 sec
Sample Input 1:
2
5 3
2 6 5 2 3
4 1
1 1 2 2
Sample Output 1:
2
1
Explanation For Sample Input 1:
In the first test case, 
The distinct pairs having difference equal to 3 are (2, 5) and (3, 6)

In the second test case, 
The distinct pairs having a difference equal to 1 is (1, 2)
Sample Input 2:
3
3 2
2 6 4
3 1
1 2 4
6 1
1 2 3 4 5 6
Sample Output 2:
2
1
5
Explanation For Sample Input 2:
In the first test case, 
The distinct pairs having difference equal to 2 are (2, 4) and (4, 6)

In the second test case, 
The distinct pairs having a difference equal to 1 is (1, 2)

In the third test case, 
The distinct pairs having difference equal to 1 are (1, 2), (2, 3), (3, 4), (4, 5) and (5, 6)
Hint

Will sorting the array help?

Approaches (2)
Sorting + Two Pointer

The brute force approach that we can think of is to check for every pair in the array and then count the number of pairs with a difference equal to ‘K’. 

 

But this brute force approach will not be valid in the case of duplicates. So, in order to handle duplicate pairs, we will sort the array first and then find the pair and finally skip the ones that are equal. 

 

Algorithm: 

  1. Sort the given array in ascending order.
  2. Create a variable let’s say “count” to store the count of pairs.
  3. Now, we will use two pointer approach to compare the values:
    • Let us say the pointers to be ‘a’ and ‘b’. Both start from ‘a’ = 0 and ‘b’ = 0
    • Start a while loop until both the pointers reach the end of the array
    • There will be three cases:
      • If arr[b] - arr[a] > ‘K’
        • The difference between element at ‘b’ and element at ‘a’ is greater than required, so to reduce it increment ‘a’.
      • If arr[b] - arr[a] < ‘K’
        • The difference between element at ‘b’ and element at ‘a’ is lesser than required, so to increase it increment ‘b’.
      • If arr[b] - arr[a] == ‘K’
        • Required pair found. Increment “count”, and skip similar elements for both ‘a’ and ‘b’.
  4. Return “count”.
Time Complexity

O(N * logN), where ‘N’ is the size of the array.

 

We need to traverse through the complete array once which takes O(N) time complexity, but sorting the array already takes O(N * logN) time complexity. So, the overall time complexity is 

O(N * logN).

Space Complexity

O(log(N)), where ‘N’ is the size of the array.

 

Since we are sorting the given array/list of integers, space complexity will be O(log(N)).

Code Solution
(100% EXP penalty)
Ninja And The Dance Competetion
Full screen
Console