-
Notifications
You must be signed in to change notification settings - Fork 0
/
#04.cpp
47 lines (36 loc) · 949 Bytes
/
#04.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
#include <iostream>
#include <cstdlib>
using namespace std;
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int moveNegToFront(int arr[], int size) {
int start = 0;
for (int i = 0; i < size; i++) {
if (arr[i] <= 0) {
swap(&arr[i], &arr[start]);
start++;
}
}
return start;
}
int lowestMissingNum(int arr[], int size) {
int start = moveNegToFront(arr, size);
// Use the array itself to track which positive numbers are present
for (int i = start; i < size; i++) {
int index = abs(arr[i]) - 1 + start;
if (index < size && arr[index] > 0) arr[index] *= -1;
}
// Return first missing number (index where value is positive)
for (int i = start; i < size; i++) {
if (arr[i] > 0) return i - start + 1;
}
// Return next largest number
return size - start + 1;
}
int main() {
int arr[11] = {-1, 4, 3, -100, 2, 5, 9, 8, -2, 6, 1};
cout << lowestMissingNum(arr, sizeof(arr) / sizeof(int)) << endl;
}