-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstMissingInteger.java
More file actions
47 lines (38 loc) · 854 Bytes
/
Copy pathFirstMissingInteger.java
File metadata and controls
47 lines (38 loc) · 854 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
35
36
37
38
39
40
41
42
43
44
45
46
47
public class FirstMissingInteger {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int firstMissingPositive(int[] A) {
for(int i=0; i<A.length; i++)
{
if(A[i] < 0) A[i] = 0;
}
for(int i=0; i<A.length; i++)
{
int abs = Math.abs(A[i]);
if(abs ==0 || abs > A.length || A[abs - 1] < -1*A.length)
{
continue;
}
else
{
A[abs-1] = -1*Math.abs(A[abs-1]);
if(A[abs-1] ==0)
{
A[abs-1] = -1*(A.length + 1);
}
}
}
for(int i=0; i<A.length; i++)
{
if(A[i]>=0)
{
return i+1;
}
}
return A.length+1;
}
}