Java Equivalent of C++’s lower_bound() Method
Last Updated :
03 May, 2023
The lower_bound() method of C++ returns the index of the first element in the array which has a value not less than the key. This means that the function returns the index of the next smallest number just greater than or equal to that number. If there are multiple values that are equal to the number, lower_bound() returns the index of the first such value.
Examples:
Input : 4 6 10 12 18 18 20 20 30 45
Output : lower_bound for element 18 at index 4
Input : 4 6 10 12 16 20 28
Output : lower_bound for element 18 at index 5
Input : 24 26 40 56
Output : lower_bound for element 18 at index 0
Input : 4 6 10 12 16 17
Output : lower_bound for element 18 at index 6
Now let us discuss the methods in order to use lower_bound() method in order to get the index of the next smallest number just greater than or equal to that number.
Methods:
- Naive Approach
- Using binary search iteratively
- Using binary search recursively
- Using binarySearch() method of Arrays utility class
Method 1: Using linear search
We can use linear search to find lower_bound. We will iterate over the array starting from the 0th index until we find a value equal to or greater than the key.
Below is the implementation of the above approach:
Java
import java.util.Arrays;
class GFG {
static int lower( int array[], int key)
{
int lowerBound = 0 ;
while (lowerBound < array.length) {
if (key > array[lowerBound])
lowerBound++;
else
return lowerBound;
}
return lowerBound;
}
public static void main(String[] args)
{
int array[]
= { 4 , 6 , 10 , 12 , 18 , 18 , 20 , 20 , 30 , 45 };
int key = 18 ;
Arrays.sort(array);
System.out.println(lower(array, key));
}
}
|
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(1)
We can use an efficient approach of binary search to search the key in the sorted array in O(log2 n) as proposed in the below example
Method 2: Using binary search iteratively
Procedure:
- Initialize the low as 0 and high as N.
- Compare key with the middle element(arr[mid])
- If the middle element is greater than or equal to the key then update the high as a middle index(mid).
- Else update low as mid + 1.
- Repeat step 2 to step 4 until low is less than high.
- After all the above steps the low is the lower_bound of a key in the given array.
Below is the implementation of the above approach:
Java
import java.util.Arrays;
public class GFG {
static int lower_bound( int array[], int key)
{
int low = 0 , high = array.length;
int mid;
while (low < high) {
mid = low + (high - low) / 2 ;
if (key <= array[mid]) {
high = mid;
}
else {
low = mid + 1 ;
}
}
if (low < array.length && array[low] < key) {
low++;
}
return low;
}
public static void main(String[] args)
{
int array[]
= { 4 , 6 , 10 , 12 , 18 , 18 , 20 , 20 , 30 , 45 };
int key = 18 ;
Arrays.sort(array);
System.out.println(lower_bound(array, key));
}
}
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Now as usual optimizing further away by providing a recursive approach following the same procedure as discussed above.
Method 3: Using binary search recursively
Java
import java.util.Arrays;
public class GFG {
static int recursive_lower_bound( int array[], int low,
int high, int key)
{
if (low > high) {
return low;
}
int mid = low + (high - low) / 2 ;
if (key <= array[mid]) {
return recursive_lower_bound(array, low,
mid - 1 , key);
}
return recursive_lower_bound(array, mid + 1 , high,
key);
}
static int lower_bound( int array[], int key)
{
int low = 0 , high = array.length;
return recursive_lower_bound(array, low, high, key);
}
public static void main(String[] args)
{
int array[]
= { 4 , 6 , 10 , 12 , 18 , 18 , 20 , 20 , 30 , 45 };
int key = 18 ;
Arrays.sort(array);
System.out.println(lower_bound(array, key));
}
}
|
Time Complexity: O(logN)
Auxiliary Space: O(logN)
Method 4: Using binarySearch() method of Arrays utility class
We can also use the in-built binary search implementation of the Arrays utility class (or Collections utility class). The function returns an index of the search key, if it is contained in the array; otherwise, (-(insertion point) – 1). The insertion point is defined as the point at which the key would be inserted into the array.
Approach:
- Sort the array before applying binary search
- Search the index of the key in the sorted array using Arrays.binarysearch()
- Check if it key is present in the array, if true then return the index of the key as a positive value.
- Otherwise, a negative value which specifies the position at which the key should be added to the sorted array.
- If the key is present in the array we move leftwards to find its first occurrence
- else, we would have got a negative value of an index, using that to calculate the value of the “insertion point” (i.e, the index of the first element greater than the key)
- Print it.
Below is the implementation of the above approach:
Java
import java.util.Arrays;
public class GFG {
static int lower_bound( int array[], int key)
{
int index = Arrays.binarySearch(array, key);
if (index < 0 ) {
return Math.abs(index) - 1 ;
}
else {
while (index > 0 ) {
if (array[index - 1 ] == key)
index--;
else
return index;
}
return index;
}
}
public static void main(String[] args)
{
int array[]
= { 4 , 6 , 10 , 12 , 18 , 18 , 20 , 20 , 30 , 45 };
int key = 18 ;
Arrays.sort(array);
System.out.println(lower_bound(array, key));
}
}
|
Best Time Complexity: O(logN)
Worst Time Complexity : O(n) , when all the elements of the array are same
Auxiliary Space: O(1)
Note: We can also find mid-value via any one of them
int mid = (high + low)/ 2;
int mid = (low + high) >> 1;
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...