forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallestMissingNumber.java
56 lines (52 loc) · 1.99 KB
/
SmallestMissingNumber.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.rampatra.arrays;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 8/29/15
* @time: 12:51 PM
*/
public class SmallestMissingNumber {
/**
* Modified Binary Search to find the smallest missing number in an array
* {@param a} consisting of numbers between 0 to m - 1 and m > n where n is
* length of array.
* <p/>
* Time complexity: O(log n)
* Con: Doesn't work if there are repetitive elements.
* <p/>
* EXPLANATION:
* In standard Binary Search, the element to be searched is compared with
* the middle element and on the basis of comparison result, we decide whether
* to search is over or to go to left half or right half.
* In this method, we modify the standard Binary Search algorithm to compare the
* middle element with its index and make decision on the basis of this comparison.
*
* @param a
* @param low
* @param high
* @return
*/
public static int smallestMissingNumber(int[] a, int low, int high) {
if (low <= high) {
int mid = (low + high) / 2;
if (a[mid] == mid) {
return smallestMissingNumber(a, mid + 1, high);
} else if (a[mid] > mid) {
return smallestMissingNumber(a, low, mid - 1);
} else {
return smallestMissingNumber(a, mid + 1, high);
}
} else {
return low;
}
}
public static void main(String[] args) {
System.out.println(smallestMissingNumber(new int[]{0, 1}, 0, 1));
System.out.println(smallestMissingNumber(new int[]{0, 1, 2, 6, 9}, 0, 4));
System.out.println(smallestMissingNumber(new int[]{4, 5, 10, 11}, 0, 3));
System.out.println(smallestMissingNumber(new int[]{0, 4, 5, 10, 56}, 0, 4));
System.out.println(smallestMissingNumber(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 10}, 0, 8));
System.out.println(smallestMissingNumber(new int[]{0, 1, 2, 3, 3}, 0, 4)); // doesn't work
}
}