Skip to content

Commit ccef2d6

Browse files
authored
Create 41. First Missing Positive (#440)
2 parents 1470dbc + cdbcb1e commit ccef2d6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

41. First Missing Positive

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int firstMissingPositive(std::vector<int>& nums) {
4+
int n = nums.size();
5+
unordered_map<int,bool> mp;
6+
// Finding the maximum element in nums
7+
int maxi = *max_element(nums.begin(), nums.end());
8+
// Populating the unordered_map with values from nums
9+
for(auto &num : nums){
10+
mp[num] = true;
11+
}
12+
// Checking for the first missing positive integer
13+
for(int i=1; i<maxi; i++){
14+
if(mp.find(i) == mp.end())
15+
return i;
16+
}
17+
// If all integers from 1 to maxi are present, return maxi+1
18+
return maxi < 0 ? 1 : maxi+1;
19+
}
20+
};

0 commit comments

Comments
 (0)