-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_4.cpp
More file actions
81 lines (67 loc) · 2.29 KB
/
problem_4.cpp
File metadata and controls
81 lines (67 loc) · 2.29 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
74
75
76
77
78
79
80
81
// Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
// For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
// You can modify the input array in-place.
// Example:
// int array[4] = { -100, -98, -99, 1 };
// const auto size = sizeof(array) / sizeof(*array);
//
// auto result = problem_four(array, size);
unsigned int get_positive_numbers(int* const numbers, const unsigned int size);
int problem_four(int* const numbers, const unsigned int size)
{
const auto positive_index = get_positive_numbers(numbers, size);
// There are no positive elements in the giver array
if (positive_index == size)
return 1;
int min_value = numbers[positive_index];
for (unsigned int i = positive_index + 1; i < size; i++)
if (numbers[i] < min_value)
min_value = numbers[i];
// first possible missing element
min_value++;
for (unsigned int i = positive_index; i < size; i++)
{
bool is_found = false;
for (unsigned int j = positive_index; j < size; j++)
if (numbers[j] == min_value)
{
is_found = true;
min_value++;
break;
}
if (!is_found)
return min_value;
}
return min_value;
}
// purpose of this function is to move all positive elements to the right-hand side;
// and all negative to the left-hand side
unsigned int get_positive_numbers(int* const numbers, const unsigned int size)
{
unsigned int i = 0, j = size - 1;
while (i < j)
{
// left == positive && right == negative => swap
if (numbers[i] > 0 && numbers[j] <= 0)
{
// swap elements
numbers[i] += numbers[j];
numbers[j] = numbers[i] - numbers[j];
numbers[i] -= numbers[j];
i++; j--;
}
// left == positive && right == positive => keep searching for negative elements on the right-hand side
else if (numbers[i] > 0)
{
j--;
}
// left == negative && right in [positive, negative] => negative element is to the left of another element
// (eigher positive or negative, we don't care), keep searching fo positive elements on the left-hand side
else
{
i++;
}
}
// i and i + 1 is the border between the positive and negative numbers
return numbers[i] > 0 ? i : i + 1;
}