-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst_Missing_Positive.java
40 lines (33 loc) · 1.12 KB
/
First_Missing_Positive.java
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
35
36
37
38
39
40
package AllCodes;
public class First_Missing_Positive {
public int firstMissingPositive(int[] nums) {
boolean onePresent=false;
//Mark all the elements which are out of range and check presence of one
for(int i=0;i<nums.length;i++){
//Checking presence
if(nums[i]==1)
onePresent=true;
//marking elements
if(nums[i]<1 ||nums[i]>nums.length){
nums[i]=1;
}
}
//if one is not present return one
if(!onePresent)
return 1;
//Map the element with index
for(int i=0;i<nums.length;i++){
int index=Math.abs(nums[i]);
nums[index-1]=-Math.abs(nums[index-1]);
}
//find out the missing positive integer
for(int i=0;i<nums.length;i++){
//if element at current index is positive then return index+1
if(nums[i]>0){
return i+1;
}
}
//if every integer is present then return length of array+1
return nums.length+1;
}
}