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
// Java program for finding lower bound
// using linear search
// Importing Arrays utility class
import java.util.Arrays;
// Main class
class GFG {
// Method 1
// To find lower bound of given key
static int lower(int array[], int key)
{
int lowerBound = 0;
// Traversing the array using length function
while (lowerBound < array.length) {
// If key is lesser than current value
if (key > array[lowerBound])
lowerBound++;
// This is either the first occurrence of key
// or value just greater than key
else
return lowerBound;
}
return lowerBound;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Custom array input over which lower bound is to
// be operated by passing a key
int array[]
= { 4, 6, 10, 12, 18, 18, 20, 20, 30, 45 };
int key = 18;
// Sort the array using Arrays.sort() method
Arrays.sort(array);
// Printing the lower bound
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
// Java program to Find lower bound
// Using Binary Search Iteratively
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Method 1
// Iterative approach to find lower bound
// using binary search technique
static int lower_bound(int array[], int key)
{
// Initialize starting index and
// ending index
int low = 0, high = array.length;
int mid;
// Till high does not crosses low
while (low < high) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is less than or equal
// to array[mid], then find in
// left subarray
if (key <= array[mid]) {
high = mid;
}
// If key is greater than array[mid],
// then find in right subarray
else {
low = mid + 1;
}
}
// If key is greater than last element which is
// array[n-1] then lower bound
// does not exists in the array
if (low < array.length && array[low] < key) {
low++;
}
// Returning the lower_bound index
return low;
}
// Method 2
// Driver main method
public static void main(String[] args)
{
// Custom array and key input over which lower bound
// is computed
int array[]
= { 4, 6, 10, 12, 18, 18, 20, 20, 30, 45 };
int key = 18;
// Sort the array using Arrays.sort() method
Arrays.sort(array);
// Printing the lower bound
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
// Java program to Find Lower Bound
// Using Binary Search Recursively
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Method 1
// To find lower bound using binary search technique
static int recursive_lower_bound(int array[], int low,
int high, int key)
{
// Base Case
if (low > high) {
return low;
}
// Find the middle index
int mid = low + (high - low) / 2;
// If key is lesser than or equal to
// array[mid] , then search
// in left subarray
if (key <= array[mid]) {
return recursive_lower_bound(array, low,
mid - 1, key);
}
// If key is greater than array[mid],
// then find in right subarray
return recursive_lower_bound(array, mid + 1, high,
key);
}
// Method 2
// To compute the lower bound
static int lower_bound(int array[], int key)
{
// Initialize starting index and
// ending index
int low = 0, high = array.length;
// Call recursive lower bound method
return recursive_lower_bound(array, low, high, key);
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// Custom array and key over which lower bound is to
// be computed
int array[]
= { 4, 6, 10, 12, 18, 18, 20, 20, 30, 45 };
int key = 18;
// Sorting the array using Arrays.sort() method
Arrays.sort(array);
// Printing the lower bound
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
// Java program to find lower bound
// using binarySearch() method of Arrays class
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Method 1
// To find lower bound using binary search
// implementation of Arrays utility class
static int lower_bound(int array[], int key)
{
int index = Arrays.binarySearch(array, key);
// If key is not present in the array
if (index < 0) {
// Index specify the position of the key
// when inserted in the sorted array
// so the element currently present at
// this position will be the lower bound
return Math.abs(index) - 1;
}
// If key is present in the array
// we move leftwards to find its first occurrence
else {
// Decrement the index to find the first
// occurrence of the key
while (index > 0) {
// If previous value is same
if (array[index - 1] == key)
index--;
// Previous value is different which means
// current index is the first occurrence of
// the key
else
return index;
}
return index;
}
}
// Method 2
// Main driver method
public static void main(String[] args)
{
//
int array[]
= { 4, 6, 10, 12, 18, 18, 20, 20, 30, 45 };
int key = 18;
// Sort the array before applying binary search
Arrays.sort(array);
// Printing the lower bound
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;
Similar Reads
Java Equivalent of C++âs upper_bound() Method
In this article, we will discuss Java's equivalent implementation of the upper_bound() method of C++. This method is provided with a key-value which is searched in the array. It returns the index of the first element in the array which has a value greater than key or last if no such element is found
8 min read
TreeSet lower() method in Java
The lower(E ele) method of TreeSet class in Java is used to return the greatest element in this set which is strictly less than the given element. If no such element exists in this TreeSet collection then this method returns a NULL. Here, E is the type of the elements maintained by this collection.
2 min read
set::lower_bound() Function in C++ STL
The std::set::lower_bound() method is used to find the first element in the set that is equal to or greater than the given value. It is a member function of std::set class and is defined inside <set> header file. In this article, we will learn about std::set::lower_bound() function in C++.Exam
3 min read
std::lower_bound in C++
In C++, the std::lower_bound() is a built-in function used to find the position of an element in a sorted range that has a value not less than the given value. It is defined inside the <algorithm> header file. In this article, we will learn about std::lower_bound() function in C++.Example:C++/
7 min read
map::lower_bound() in C++ STL
In C++, std::map::lower_bound() is a built-in method used to find the first element in the map whose key is either equal to or greater than the given key. In this article, we will learn about std::map::lower_bound() function in C++.Example:C++// C++ Program to illustrate the use of // std::map::lowe
4 min read
multiset lower_bound() in C++ STL with Examples
The multiset::lower_bound() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter. In case k is not present in the set container, the function returns an iterator pointing to the immediate next elem
3 min read
NavigableSet lower() method in Java
The lower() method of NavigableSet interface in Java is used to return the greatest element in this set strictly less than the given element, or null if there is no such element exists in the set. Syntax: E lower(E ele) Where, E is the type of elements maintained by this Set container. Parameters: T
2 min read
ConcurrentSkipListSet lower() method in Java with Examples
The lower() method of ConcurrentSkipListSet is used to return the largest element present in this set which is strictly less than the specified element. If there is no such element present in the set, then this function will return null. Syntax: public E lower (E e) Parameters: This method takes onl
2 min read
Integer lowestOneBit() Method in Java
The Integer.lowestOneBit() method of java.lang is an inbuilt function that returns an int value with at most a single one-bit, in the position of the lowest-order (ie.rightmost) one-bit in the specified int value. This method will return zero if the specified value has no one-bits in its two's compl
3 min read
multimap lower_bound() function in C++ STL
The multimap::lower_bound(k) is a built-in function in C++ STL which returns an iterator pointing to the key in the container which is equivalent to k passed in the parameter. In case k is not present in the multimap container, the function returns an iterator pointing to the immediate next element
2 min read