-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindRange.java
More file actions
39 lines (37 loc) · 1.56 KB
/
FindRange.java
File metadata and controls
39 lines (37 loc) · 1.56 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
class FindRange {
public static int[] findRange(int[] arr, int key) {
int[] result = new int[] { -1, -1 };
result[0] = search(arr, key, false);
if (result[0] != -1) // no need to search, if 'key' is not present in the input array
result[1] = search(arr, key, true);
return result;
}
// modified Binary Search
private static int search(int[] arr, int key, boolean findMaxIndex) {
int keyIndex = -1;
int start = 0, end = arr.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (key < arr[mid]) {
end = mid - 1;
} else if (key > arr[mid]) {
start = mid + 1;
} else { // key == arr[mid]
keyIndex = mid;
if (findMaxIndex)
start = mid + 1; // search ahead to find the last index of 'key'
else
end = mid - 1; // search behind to find the first index of 'key'
}
}
return keyIndex;
}
public static void main(String[] args) {
int[] result = FindRange.findRange(new int[] { 4, 6, 6, 6,6,6, 9 }, 6);
System.out.println("Range: [" + result[0] + ", " + result[1] + "]");
result = FindRange.findRange(new int[] { 1, 3, 8, 10, 15 }, 10);
System.out.println("Range: [" + result[0] + ", " + result[1] + "]");
result = FindRange.findRange(new int[] { 1, 3, 8, 10, 15 }, 12);
System.out.println("Range: [" + result[0] + ", " + result[1] + "]");
}
}