forked from skylarbpayne/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicate_within_k.cpp
More file actions
39 lines (35 loc) · 834 Bytes
/
duplicate_within_k.cpp
File metadata and controls
39 lines (35 loc) · 834 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
/**
* Author: Skylar Payne
* Date: January 5, 2014
* Check if an array has any duplicate elements within a distance of k
**/
#include <iostream>
#include <vector>
#include <set>
#include <stdlib.h>
bool duplicates_within_k(std::vector<int> const& arr, int k) {
std::set<int> window;
for(int i = 0; i < arr.size(); ++i) {
if(window.find(arr[i]) != window.end()) {
return true;
}
window.insert(arr[i]);
if(i >= k) {
window.erase(arr[i-k]);
}
}
return false;
}
int main(int argc, char** argv) {
if(argc < 3) {
std::cout << "Please provide k and a list of integers" << std::endl;
return -1;
}
int k = atoi(argv[1]);
std::vector<int> arr(argc - 2);
for(int i = 2; i < argc; ++i) {
arr[i-2] = atoi(argv[i]);
}
std::cout << (duplicates_within_k(arr, k) ? "True" : "False") << std::endl;
return 0;
}