-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFirst Missing Integer.cpp
More file actions
34 lines (28 loc) · 941 Bytes
/
First Missing Integer.cpp
File metadata and controls
34 lines (28 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//have to find the first missing positive number which is n't there in the array. so I seperated {positive},{0,negative} numbers seperately by swapping
int Solution::firstMissingPositive(vector<int> &A) {
int n = A.size();
int j = 0;
for(int i =0;i<n;i++){
if(A[i]<=0){
swap(A[j],A[i]);
j++;
}
}
//start of +ve numbers
int st = j;
//maximum +ve numbers to be present
int cnt = n-st;
//Do the standard procedure (make the corresponding elements in corresponding array indexes -ve if they are present in this array.
for(int i = st;i<n;i++){
if(abs(A[i]) <= cnt){
A[st+abs(A[i])-1] = -abs(A[st+abs(A[i])-1]);
}
}
//check the missing element in the array by the above mentioned procedure.
for(int i = st;i<n;i++){
if(A[i]>=0){
return i-st+1;
}
}
return cnt+1;
}