-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.java
29 lines (28 loc) · 1.15 KB
/
temp.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
public class Main {
boolean binarySearch(int[] array, int target, int l, int r) {
if (l > r) return -1;
int h = (l+r)/2;
if (array[h] == target) return true;
if (target < array[h]) return binarySearch(array, target, l, h-1);
if (target > array[h]) return binarySearch(array, target, h+1, r);
return false; // this line doesn't matter, just to have something to return
}
// Returns the lowest index where you could insert an element in an array
int lowerIndex(int[] array, int target, int l, int r) {
if (l > r) return l;
int h = (l+r)/2;
if (target == array[h]) return ...;
if (target < array[h]) return ...;
if (target > array[h]) return ...;
return false; // doesn't matter again
}
// Returns the highest index where you could insert an element in an array
int upperIndex(int[] array, int target, int l, int r) {
if (l > r) return l;
int h = (l+r)/2;
if (target == array[h]) return ...;
if (target < array[h]) return ...;
if (target > array[h]) return ...;
return false; // doesn't matter again
}
}