Type Here to Get Search Results !

# Count Pairs | TCS CodeVita 9 Solution ( Zone 1 ) 2020 | By CodingHumans |

3

Count Pairs

Problem Description

Given an array of integers A, and an integer K find number of happy elements.

Element X is happy if there exists at least 1 element whose difference is less than K i.e. an element X is happy,
if there is another element in the range [X-K, X+K] other than X itself.

Constraints

1 <= N <= 10^5

0 <= K <= 10^5

0 <= A[i] <= 10^9

Input

First line contains two integers N and K where N is size of the array and K is a number as described above

Second line contains N integers separated by space.

Output

Print a single integer denoting the total number of happy elements.

Time Limit

1

Examples
Example 1

Input

6 3
5 5 7 9 15 2

Output

5

Explanation

Other than number 15, everyone has at least 1 element in the range [X-3, X+3].
Hence they are all happy elements. Since these five are in number, the output is 5.

Example 2

Input

3 2
1 3 5

Output

3

Explanation

All numbers have at least 1 element in the range [X-2, X+2]. Hence they are all happy elements.
Since these three are in number, the output is 3.

Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAPPY CODING 😁

Solution
( C ++ )

If you have any doubts regarding this problem or  need the solution in other programming languages then leave a comment down below .

1. can you send the same code in python
and does the ques is like from ex 1 for 5 the range [5-3=2,5+3=8] and 7 and 2 is in this range is this a proper approach

2. This solution works fine😊

3. import java.util.*;
class B{
public static void main(String ar[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int count=0,i;
int arr[] = new int[n];
B b = new B();
for(i=0;i=l) && (ar[j]<= h))
{
return 1;
}
}
}
return 0;
}
}