[Lower Bound]
static int lower_bound(ArrayList<Integer> arr, int target) { int mid, start, end; start = 0; end = arr.size()-1;
while (end > start) { mid = (start + end) / 2; if (arr.get(mid) >= target) end = mid; else { start = mid + 1; } } return end; } |
[Upper Bound]
static int upper_bound(ArrayList<Integer> arr, int target) { int mid, start, end; start = 0; end = arr.size()-1;
while (end > start) { mid = (start + end) / 2; if (arr.get(mid) > target) { end = mid; } else start = mid + 1; } return end; } |