-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathM0023.cpp
More file actions
38 lines (32 loc) · 677 Bytes
/
M0023.cpp
File metadata and controls
38 lines (32 loc) · 677 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
/*
Problem Statement: https://www.hackerrank.com/challenges/hackerland-radio-transmitters/problem
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int hackerlandRadioTransmitters(int k, vector<int> &x) {
int n, i, pos, transmitters;
n = x.size();
transmitters = i = 0;
sort(x.begin(), x.end());
while (i < n) {
pos = x[i] + k;
while (i < n && x[i] <= pos)
i++;
pos = x[--i] + k;
while (i < n && x[i] <= pos)
i++;
transmitters++;
}
return transmitters;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> x(n);
for (int i = 0; i < n; i++)
cin >> x[i];
cout << hackerlandRadioTransmitters(k, x);
return 0;
}