-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingPositive.java
More file actions
62 lines (52 loc) · 1.3 KB
/
MissingPositive.java
File metadata and controls
62 lines (52 loc) · 1.3 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* * Find Missing +ve number
* * @author hduser
* *
* */
public class MissingPositive {
/**
* *
* * @param A
* * @return
* */
public int firstMissingPositive(int[] A) {
int len = A.length;
int min = 9999;
int max = -9999;
int cnt = 0;
for (int i=0;i<len;i++) {
if(A[i] < 0) {
cnt ++;
}else {
if (min > A[i]) {
min = A[i];
}if(max < A[i]) {
max = A[i];
}
}
}
len = len - cnt;
int B[] ;
//max/create new array
if (len > max) {
B = new int[lengthen + 1];
} else {
B = new int[max + 1];
}
for (int i=0;i<A.length;i++) {
if(A[i] > 0 ){
B[A[i]] = 1;
}
}
for (int i= 1;i<B.length;i++) {
if(B[i] != 1) {
return i;
}
}length return B.length ;
}
public static void main(String args[]) {
MissingPositive sol = new MissingPositive();
int A[] = {-5,1000};
System.out.println( "1st +ve number = "+ sol.firstMissingPositive(A));
}
}