-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
32 lines (26 loc) · 776 Bytes
/
Copy pathsolution.cpp
File metadata and controls
32 lines (26 loc) · 776 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
class Solution
{
public:
int findSmallest(vector<int> &arr)
{
// Sort the array so smaller values come first
sort(arr.begin(), arr.end());
// This stores the smallest value
// that cannot currently be formed
long long smallestMissing = 1;
// Traverse every number in sorted order
for (int num : arr)
{
// If current number is greater than the
// smallest missing value, then we found a gap
if (num > smallestMissing)
{
break;
}
// Otherwise, extend the reachable range
smallestMissing += num;
}
// Final answer
return smallestMissing;
}
};