-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclosest_k.cpp
More file actions
61 lines (48 loc) · 1.34 KB
/
closest_k.cpp
File metadata and controls
61 lines (48 loc) · 1.34 KB
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
61
//
// Created by Mayank Parasar on 2020-05-06.
//
/*
* Hi, here's your problem today. This problem was recently asked by AirBNB:
Given a list of sorted numbers, and two integers k and x, find k closest numbers to the pivot x.
Here's an example and some starter code:
def closest_nums(nums, k, x):
# Fill this in.
print(closest_nums([1, 3, 7, 8, 9], 3, 5))
# [7, 3, 8]
*/
#include <iostream>
#include <vector>
using namespace std;
vector<int> k_closest_neighbours(vector<int> arr, int k, int x) {
vector<int> result;
for(auto i : arr) {
if( result.size() < k) {
result.push_back(i);
}
else {
int max_diff = -1;
int curr_diff;
int idx;
for(int ii = 0; ii < result.size(); ii++) {
curr_diff = abs(result[ii] - x);
if((curr_diff > max_diff)) {
max_diff = curr_diff;
idx = ii;
}
}
if(max_diff > abs(x-i)) {
result[idx] = i;
}
}
}
return result;
}
int main() {
vector<int> arr = {1, 3, 7, 8, 9}; // given that this array is sorted
// vector<int> arr1 = {6, 7, 8, 9, 5};
int k = 3;
int x = -1;
vector<int> result = k_closest_neighbours(arr, k, x);
for(auto i : result)
cout << i << " ";
}