-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCount Pairs.cpp
49 lines (38 loc) · 1.37 KB
/
Count Pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Code by Nikhil Mohan
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void test()
{
ll n,k,l;
scanf("%lld",&n);
set<ll> s; // to remove duplicate elements from array (we distinct elements only)
map<ll,int> 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); //insertion in to set
m[l]++; // counting frequency for element l
}
//copying the set to array a(to acess previous, current and next element at a time)
// set by default sort the input , we dont need to sort the array
n=s.size();
ll a[n],i=0,count=0;
for(ll x : s) {a[i]=x;
i++; }
// loop to check (adjacent diff <=k )
//since we dont have previous element for a[0] , we keep a separatle check for this (jst to avoid runtime error)
if(a[1]-a[0]<=k) count+=m[a[0]];
for(int i=1;i<n-1;i++)
{
// when this condition satisfies we increment the count by that element frequency.
if( a[i+1]-a[i]<=k || abs(a[i]-a[i-1])<=k) count+=m[a[i]];
}
//since we dont have next element for a[n-1] , we keep a separatle check for this (jst to avoid runtime error)
if(a[n-1]-a[n-2]<=k) count+=m[a[n-1]];
// printing output
cout<<count;
}
//driver code
int main()
{ test();
}