-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHACKRNDM.cpp
More file actions
41 lines (34 loc) · 801 Bytes
/
HACKRNDM.cpp
File metadata and controls
41 lines (34 loc) · 801 Bytes
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
#include <stdio.h>
#include <algorithm>
using namespace std;
int binarySearch(int arr[], int startIndex, int endIndex, int element) {
if (startIndex > endIndex) {
return 0;
}
int mid = startIndex + ((endIndex - startIndex) / 2);
if (arr[mid] == element) {
return 1;
}
if (element > arr[mid]) {
return binarySearch(arr, mid + 1, endIndex, element);
} else {
return binarySearch(arr, startIndex, mid - 1, element);
}
}
int main() {
int size;
scanf("%d",&size);
int arr[size];
int difference;
scanf("%d",&difference);
for (int i = 0; i < size; i++) {
scanf("%d",&arr[i]);
}
sort(arr,arr+size);
int count = 0;
for (int i = 0; i < size - 1; i++) {
count += binarySearch(arr, i + 1, size - 1, arr[i]+difference);
}
printf("%d\n",count);
return 0;
}