-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinaryS.java
More file actions
32 lines (29 loc) · 1004 Bytes
/
binaryS.java
File metadata and controls
32 lines (29 loc) · 1004 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
public class binaryS {
public static void main(String[] args){
int[] test = {1,2,2,2,2,2,4,5,6};
System.out.println("left most index: " + findIdx(test, 2, true));
System.out.println("right most index: " + findIdx(test, 2, false));
}
public static int findIdx(int[] array, int target, boolean leftMost){
int left = 0, right = array.length-1;
int middle;
while(left + 1 < right){
middle = left + (right - left) / 2;
if(array[middle] < target){
left = middle;
}else if(array[middle] > target){
right = middle;
}else{
/** The key thing is here! **/
if (leftMost){
right = middle;
}else{
left = middle;
}
}
}
if(array[left] == target) return left;
else if(array[right] == target) return right;
else return -1;
}
}