-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
66 lines (57 loc) · 1.54 KB
/
solution.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
61
62
63
64
65
66
// you can use includes, for example:
// #include <algorithm>
#include <set>
#include <vector>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
set<int> convertToSet(vector<int>& v)
{
// Declaring the set
// using range of vector
set<int> s(v.begin(), v.end());
// Return the resultant Set
return s;
}
void printSet(set<int>& s)
{
cout << "Set: ";
for (int x : s) {
cout << x << " ";
}
cout << endl;
}
int solution(vector<int>& A)
{
// write your code in C++14 (g++ 6.2.0)
int largestInSet = 0;
int smallestPositiveInteger = 0;
set<int> sortedSet = convertToSet(A);
largestInSet = *sortedSet.rbegin();
if (largestInSet < 0)
{
smallestPositiveInteger = 1;
}
else
{
bool conditionMet = false;
int nextValue = largestInSet - 1;
while (conditionMet != true)
{
if (sortedSet.count(nextValue) == 0)
{
smallestPositiveInteger = nextValue;
if (smallestPositiveInteger == 0)
{
smallestPositiveInteger = largestInSet + 1;
}
break;
}
nextValue = nextValue - 1;
}
}
//cout << "largest:" << largestInSet << endl;
//printSet(sortedSet);
return smallestPositiveInteger;
}