Java have already built-in binary search functionality that calculates lower/upper bounds for an element in an array, there is no need to implement custom methods.
When we speak about upper/lower bounds or equal ranges, we always mean indexes of a container (in this case of ArrayList), and not the elements contained.
Let's consider an array (we assume the array is sorted, otherwise we sort it first):
List<Integer> nums = new ArrayList<>(Arrays.asList(2,3,5,5,7,9,10,18,22));
The "lower bound" function must return the index of the array, where the element must be inserted to keep the array sorted. The "upper bound" must return the index of the smallest element in the array, that is bigger than the looked for element.
For example
lowerBound(nums, 6)
must return 3, because 3 is the position of the array (starting counting with 0), where 6 must be inserted to keep array sorted.
The
upperBound(nums, 6)
must return 4, because 4 is the position of the smallest element in the array, that is bigger than 5 or 6, (number 7 on position 4).
In C++ in standard library the both algorithms already implemented in standard library. In Java you can use
Collections.binarySearch(nums, element)
to calculate the position in logarithmic time complexity.
If the array contains the element, Collections.binarySearch returns the first index of the element (in the array above 2). Otherwise it returns a negative number that specifies the position in the array of the next bigger element, counting backwards from the last index of the array. The number found in this position is the smallest element of the array that is bigger than the element you look for.
For example, if you call
int idx = Collections.binarySearch(nums, 6)
the function returns -5. If you count backwards from the last index of the array (-1, -2, ...) the index -5 points to number 7 - the smallest number in the array that is bigger than the element 6.
Conclusion:
if the sorted array contains the looked for element, the lower bound is the position of the element, and the upper bound is the position of the next bigger element.
If the array does not contains the element, the lower bound is the position
Math.abs(idx) - 2
and the upper bound is the position
Math.abs(idx) - 1
where
idx = Collections.binarySearch(nums, element)
And please always keep in mind the border cases. For example, if you look for 1 in the above specified array:
idx = Collections.binarySearch(nums, 1)
The functon returns -1. So, the upperBound = Math.abs(idx) - 1 = 0 - the element 2 at position 0. But there is no lower bound for element 1, because 2 is the smallest number in the array.
The same logic applies to elements bigger than the biggest number in the array: if you look for lower/upper bounds of number 25, you will get
idx = Collections.binarySearch(nums, 25)
ix = -10. You can calculate the lower bound : lb = Math.abs(-10) - 2 = 8, that is the last index of the array, but there is no upper bound, because 22 is already biggest element in the array and there is no element at position 9.
The equal_range specifies all indexes of the array in the range starting from the lower bound index up to (but not including) the upper bound.
For example, the equal range of number 5 in the array above are indexes
[2,3]
Equal range of number 6 is empty, because there is no number 6 in the array.