# Count Pairs Codevita 9 Solution

Page Contents

## Count Pairs TCS Codevita 9 Solution 2020

Count Pairs

Problem Description

Given a variety of numbers An, and a whole number K discover number of glad components.

Component X is glad if there exists in any event 1 component whose distinction is not as much as K for example a component X is glad,

on the off chance that there is another component in the range [X-K, X+K] other than X itself.

Imperatives

1 <= N <= 10^5

0 <= K <= 10^5

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

Information

First line contains two whole numbers N and K where N is size of the exhibit and K is a number as depicted previously

Second line contains N whole numbers isolated by space.

Yield

Print a solitary whole number meaning the all out number of upbeat components.

Time Limit

Models

Model 1

Information

6 3

5 7 9 15 2

Yield

Clarification

Other than number 15, everybody has in any event 1 component in the range [X-3, X+3].

Consequently they are for the most part glad components. Since these five are in number, the yield is 5.

Model 2

Information

3 2

1 3 5

Yield

Clarification

All numbers have in any event 1 component in the range [X-2, X+2]. Thus they are on the whole upbeat components.

Since these three are in number, the yield is 3.

### SOLUTION OF COUNT PAIRS :-

include

using namespace std;
typedef long long ll;

void test()
{
ll n,k,l;
scanf(“%lld”,&n);

set s; // to remove duplicate elements from array
map m; // to keep track of frequency of each element in given array

for(int i=0;i<n;i++)
{scanf(“%lld”,&l);
s.insert(l);
m[l]++;
}

//copying the set to array a
n=s.size();
ll a[n],i=0,count=0;

for(ll x : s) {a[i]=x;
i++; }

// loop to check (adjacent diff <=k )

if(a-a<=k) count+=m[a];
for(int i=1;i<n-1;i++)
{
if( a[i+1]-a[i]<=k || abs(a[i]-a[i-1])<=k) count+=m[a[i]]; // when this condition satisfies we increment the count by that element frequency.
}

if(a[n-1]-a[n-2]<=k) count+=m[a[n-1]];

cout<<count; // printing output

}
int main()
{ test();

}

//Code by CYBER GEEK