Skip to content

Create 41. First Missing Positive #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions 41. First Missing Positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int firstMissingPositive(std::vector<int>& nums) {
int n = nums.size();
unordered_map<int,bool> mp;
// Finding the maximum element in nums
int maxi = *max_element(nums.begin(), nums.end());
// Populating the unordered_map with values from nums
for(auto &num : nums){
mp[num] = true;
}
// Checking for the first missing positive integer
for(int i=1; i<maxi; i++){
if(mp.find(i) == mp.end())
return i;
}
// If all integers from 1 to maxi are present, return maxi+1
return maxi < 0 ? 1 : maxi+1;
}
};
Loading