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 4Input : 4 6 10 12 16 20 28
Output : lower_bound for element 18 at index 5Input : 24 26 40 56
Output : lower_bound for element 18 at index 0Input : 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)); } } |
4
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)); } } |
4
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)); } } |
4
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)); } } |
4
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;