We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1470dbc commit cdbcb1eCopy full SHA for cdbcb1e
41. First Missing Positive
@@ -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