forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstMissingPositiveNum.cpp
75 lines (65 loc) · 2.13 KB
/
firstMissingPositiveNum.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
67
68
69
70
71
72
73
74
75
/**
* Problem: Given an unsorted integer array, find the first missing positive integer.
*
* For example,
* Given [1,2,0] return 3,
* and [3,4,-1,1] return 2.
*
* Your algorithm should run in O(n) time and uses constant space.
*
* Approach :
* Step 1 :Iterate through array and :
* - Ignore non positive numbers.
* - For a positive number, if we have A[i] = 'x', then in a sorted array in original scheme of things,
* it should be placed at A[x-1] ( since array is 0 indexed), so we swap A[i] with A[x-1].
* Step 2: Iterate through array and now compare each value with their index, i.e. num[i] == i + 1.
* - Whereever this check fails, that is the number at that index is missing, ( i.e. if A[i] is at wrong place, means i+1 is missing).
*/
#include <iostream>
#include <vector>
int firstMissingPositive(std::vector<int>& nums) {
if( nums.size() == 0 ) {
return 1;
}
size_t i = 0;
for ( i = 0; i < nums.size(); ++i) {
while(nums[i] > 0 && nums[i] < int(nums.size()) && nums[i] != int(i+1) && nums[i] != nums[nums[i] - 1]) {
std::swap(nums[i], nums[nums[i]-1]);
}
}
i = 0;
while( i < nums.size() && nums[i] == int(i + 1)) {
++i;
}
return int(i+1);
}
void printVec(std::vector<int>& nums) {
for ( auto num : nums ) {
std::cout << num << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> vec1{ 1, 2, 0 };
std::cout << "Vector:";
printVec(vec1);
std::cout << "First missing positive num :" << firstMissingPositive(vec1) << std::endl;
std::vector<int> vec2{ 1000, -1 };
std::cout << "Vector:";
printVec(vec2);
std::cout << "First missing positive num :" << firstMissingPositive(vec2) << std::endl;
std::vector<int> vec3{ 3,4,-1,1 };
std::cout << "Vector:";
printVec(vec3);
std::cout << "First missing positive num :" << firstMissingPositive(vec3) << std::endl;
std::vector<int> vec4{ 0 };
std::cout << "Vector:";
printVec(vec4);
std::cout << "First missing positive num :" << firstMissingPositive(vec4) << std::endl;
std::vector<int> vec5{ 1 };
std::cout << "Vector:";
printVec(vec5);
std::cout << "First missing positive num :" << firstMissingPositive(vec5) << std::endl;
return 0;
}