-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cpp
49 lines (46 loc) · 1.12 KB
/
Solution.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
/*
Problem : https://www.hackerrank.com/challenges/pairs/problem
C++ 14
Approach :
We simply need to translate the problem statement into code efficiently, i.e.
Given N integers, count the number of pairs of integers whose difference is K.
We use binary search for the purpose. A linear search will run in O(n) time which
will not be sufficient to pass all the test cases.
Time Complexity : O( n*log(n) )
Space Complexity : O( n )
*/
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
long long arr[100000];
bool b_search(int l, int r, long long x){
int m;
while (l <= r){
m = l + (r-l)/2;
if (arr[m] == x)
return true;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return false;
}
int main() {
int n;
long long k, ans=0;
cin>>n>>k;
int i,j;
for(i=0; i<n; i++){
cin>>arr[i];
}
sort(arr, arr+n);
for(i=0; i<n-1; i++){
if(b_search(i+1, n-1, arr[i]+k)){
ans++;
}
}
cout<<ans;
return 0;
}