Skip to content

Commit a7e57d7

Browse files
Merge pull request #12 from thekalyan001/main
Leet code problem First Missing Positive added
2 parents 8388a57 + 0e455bc commit a7e57d7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int firstMissingPositive(vector<int>& nums) {
4+
int n=nums.size();
5+
6+
//number should be always between 1 to n
7+
for(int i=0;i<n;i++){
8+
if(nums[i]<=0 or nums[i]>n)
9+
nums[i]=n+1;
10+
}
11+
12+
//
13+
for(int i=0;i<n;i++){
14+
if(abs(nums[i])<=n){
15+
int index=abs(nums[i])-1;
16+
nums[index]=-abs(nums[index]);
17+
}
18+
}
19+
for(int i=0;i<n;i++){
20+
if(nums[i]>0)return i+1;
21+
}
22+
return n+1;
23+
}
24+
};

0 commit comments

Comments
 (0)