-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfirst-missing-positive.cpp
48 lines (38 loc) · 1.33 KB
/
first-missing-positive.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
class Solution {
// Traverse the input array and replace all zeroes and negative number by INT_MAX
// Traverse the input array again, and during this traversal we will find the correct position of each element by making value at that index negative.
// During final traversal, return the (index+1) where "index" is the first index at which element found is positive.
public:
int firstMissingPositive(vector<int>& nums) {
for( int i = 0; i < nums.size(); i++ ) {
if (nums[i] < 1) { nums[i] = INT_MAX; }
}
for( int i = 0; i < nums.size(); i++ ) {
if ( abs(nums[i]) <= nums.size()) {
nums[abs(nums[i]) - 1] = -abs(nums[abs(nums[i]) - 1]);
}
}
for(int i = 0; i < nums.size(); i++) {
if (nums[i] > 0) {
return i + 1;
}
}
return nums.size() + 1;
}
};
// Simple map based but have nlogn complexity
// class Solution {
// public:
// int firstMissingPositive(vector<int>& nums) {
// map<int,int>m;
// for(int i=0;i<nums.size();i++)
// {
// m[nums[i]]++;
// }
// for(int i=1;i<=nums.size()+1;i++){
// if(m[i]==0)
// return i;
// }
// return 1;
// }
// };