Skip to content

Commit 9fb7298

Browse files
authored
CPP version of MEX value of array in O(1) space
Check python code for more comments
1 parent e266272 commit 9fb7298

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Check Youtube video for video explanation
2+
class Solution {
3+
public:
4+
int firstMissingPositive(vector<int> A)
5+
{
6+
int n = A.size();
7+
for(int i = 0; i < n; ++ i) {
8+
while(A[i] >= 1 && A[i] <= n && A[i] != i+1)
9+
swap(A[i], A[A[i] - 1]);
10+
}
11+
12+
for(int i = 0; i < n; ++ i)
13+
if(A[i] != i + 1)
14+
return i + 1;
15+
16+
return n + 1;
17+
}
18+
};

0 commit comments

Comments
 (0)