Skip to content

Commit dc83413

Browse files
authored
Merge pull request #16 from Arp1tSharma/patch-3
Create first-missing-positive.cpp
2 parents 46998b9 + dce8a89 commit dc83413

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

first-missing-positive.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int firstMissingPositive(vector<int>& nums) {
4+
int n = nums.size();
5+
for(int i = 0 ; i < n ; i++){
6+
long curr = (long)nums[i]-1;
7+
while(nums[i] > 0 && nums[i] <= n && nums[i] != nums[curr]){
8+
swap(nums[i],nums[curr]);
9+
curr = (long)nums[i]-1;
10+
}
11+
}
12+
// for(int i : nums) cout<< i<<" ";
13+
for(int i = 0 ; i < n ; i++){
14+
if(nums[i] != i+1) return i+1;
15+
}
16+
return n+1;
17+
}
18+
};

0 commit comments

Comments
 (0)