-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskyWatching.cpp
60 lines (57 loc) · 1.46 KB
/
skyWatching.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
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//it returns a pair of no. of stars and leftmost coordinate
pair <int, int> skyWatcher(vector <int> &xCoordinates, int length) {
sort(xCoordinates.begin(), xCoordinates.end());
int i = 0;
int j = 0;
int leftMost = xCoordinates[0];
int currLeft = xCoordinates[0];
int maxStars = 1;
int currMax = 1;
while (i < xCoordinates.size() && j < xCoordinates.size()) {
if (i == j) {
j++;
}
else if (abs(xCoordinates[j] - xCoordinates[i]) <= length) {
currMax += 1;
j++;
}
else {
if (currMax >= maxStars) {
maxStars = currMax;
leftMost = currLeft;
}
i++;
currLeft = xCoordinates[i];
currMax--;
if (currMax == 0) {
currMax = 1;
}
}
}
if (currMax >= maxStars) {
maxStars = currMax;
leftMost = currLeft;
}
return make_pair(maxStars, leftMost);
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int length;
cin >> length;
vector <int> xCoordinates(n);
for (int i = 0; i < n; i++) {
cin >> xCoordinates[i];
}
pair<int, int> ans = skyWatcher(xCoordinates, length);
cout << ans.first <<" " << ans.second << endl;
}
return 0;
}