-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmall_integer_array
More file actions
73 lines (57 loc) · 1.57 KB
/
small_integer_array
File metadata and controls
73 lines (57 loc) · 1.57 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
62
63
64
65
66
67
68
69
70
71
72
73
// from codility demo.
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
/*
my algo:
discard negatives below 1.
have min, max, max-min = range,
sort,
make hashmap array? reduce duplicates.
check if #'s in range (from array)
if no gap, max + 1
else return gap
*/
int solution(vector<int> &A) {
vector<int> B = A; // copy
int size = A.size(), gap=0;
int max, min, range; // postive only
//sort for calculations
sort(B.begin(), B.end());
// for (int i = 0; i < size; i++) cout << B[i] << " "; cout << endl;
// get max/min!
max = *max_element(B.begin(), B.end());
min = *min_element(B.begin(), B.end());
std::cout << "max: " << max << " min: " << min << endl;
set <int> s;
set <int>::iterator it;
for (int x : B) { // take away duplicates
s.insert(x);
}
int flag = 0;
// if negative , case where all array is negative?
for (it = s.begin(); it != s.end(); it++){
if (*it < 0) {
gap=1;
return gap; //default to 1
}
}
for (int j = 1; j < max; j++) {
if (s.find(j) == s.end()) {
gap = j-1; // no gap
}
else {
gap = max;
}
}
cout << "gap: " << gap << endl;
return gap+1;
}
int main() {
vector <int> A = {1,2,3}; //->4, A=[-1,3] -> 1, A = [1,2,1,3,4,6] -> 5
int c = solution(A);
cout << "sol: " << c << endl;
return 0;
}