-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissinginteger.java
More file actions
58 lines (50 loc) · 1.5 KB
/
missinginteger.java
File metadata and controls
58 lines (50 loc) · 1.5 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
//Solved by Mark Davis on 08/11/2018
// worst case time complexity: O(N) to O(N * log(N))
// worst-case space complexity is O(N)
public class Solution {
public static void main(String args[])
{
int[] arr = {1, 3, 6, 4, 1, 2};
//System.out.println("length = " + arr.length);
//System.out.println("output = " + solution(arr));
}
public static int solution(int[] A) {
// write your code in Java SE 8
int N = A.length;
int max = N+1;
int[] keeps = new int[N];
//trivial solution
int min = 1;
//nontrivial solution
if (N> 0)
{
int at;
//iterate through N integers
for (int i=0; i<N; i++)
{
at = A[i];
//System.out.println("at=" + at + ", min=" + min + ", max=" + max);
//if within bounds, add to array
if(at<min || at>=max)
{
max--;
continue;
}
else
{
keeps[at-1] = at;
}
}
for (int i=0; i<N; i++)
{
//System.out.println("keeps=" + keeps[i] + ", i=" + i);
if (keeps[i]!=i+1)
{
return i+1;
}
}
return max;
}
return min;
}
}