
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Equivalent of C++'s lower_bound Method
In this problem, we will learn to implement the equivalent algorithm of C++'s lower_bound() method in Java to find the lower bound index of a given element in the sorted array.
Lower bound ? The lower bound is the index in the sorted array, such that the index contains the minimum element greater or equal to the targeted element.
We can use the searching algorithms to find the lower bound of any element in the sorted array without using built-in methods. Here, we will use the linear search, iterative, and recursive binary search to get the lower bound of any element.
Problem statement ? We have given a sorted array of numeric elements. We need to implement the equivalent of C++'s lower_bound() method in Java and return the lower bound index of the targeted element.
Using The Linear Search For The Alternative of lower_bound()
The linear search algorithm traverses the array and compares each array element with the targeted element. To find the lower bound of the particular element, we can compare each element with the targeted element, and if the current element is greater or equal to the target element, we consider the current index as a lower bound.
Algorithm
Step 1 ? Initialize the 'lb' with 0 to store the lower bound index.
Step 2 ? Start traversing the array till the 'lb' index value is less than the array's length.
Step 3 ? If the target element's value is greater than the current element, increment the 'lb' by 1.
Step 4 ? If the target element's value is less than the current element's value, return the 'lb' value.
Step 5 ? At last, return the 'lb' value. If all elements of the array are smaller than the target element, we can consider the array's length as a lower bound for the target element.
Example
import java.util.Arrays; public class Main { static int get_lower_bound(int nums[], int target) { int lb = 0; // Iterate over array elements while (lb < nums.length) { // When the target value is greater than the current array element if (target > nums[lb]) { lb++; } // For minimum value greater or equal to the target value else { return lb; } } return lb; } public static void main(String[] args) { int nums[] = { 2, 3, 5, 7, 12, 15, 17, 20, 32, 54, 67, 98 }; int target = 22; // Print index of the lower bound System.out.println("The lower bound index is for target element is " + get_lower_bound(nums, target)); } }
Output
The lower bound index is for target element is 8
Time complexity ? O(N), as we need to traverse the array.
Space complexity ? O(1), as we don't use dynamic space.
Using The Iterative Binary Search For The Alternative Of lower_bound()
The binary search is the efficient searching algorithm that searches in the subparts of the array. We divide the array into two parts from the middle. If the middle element is greater than the target element, we search for a lower bound in the left part. Otherwise, we search for the lower bound index in the right part of the array. In this way, we divide the subarray again into another subarray.
Algorithm
Step 1 ? Initialize the 'left' pointer with 0 and the 'right' pointer with the array's length.
Step 2 ? Make iterations using the while loop until the 'left' pointer value is less than the 'right' pointer's value.
Step 3 ?Find the middle pointer using the left and right pointer values.
Step 4 ? If the element at the middle index is greater or equal to the target element, update the 'right' pointer with the middle pointer.
Step 5 ? Otherwise, update the 'left' pointer with the 'middle + 1' value.
Step 6 ? If the 'left' pointer is less than the array's length, and the element at the 'left' index is less than the target element, increment 'left' by 1.
Step 7 ? Return the 'left' element's value.
Example
import java.util.Arrays; public class Main { static int get_lower_bound(int nums[], int target) { int left = 0, right = nums.length; int middle; // Traverse array while (left < right) { // Get the middle element middle = left + (right - left) / 2; // For finding the element in the left subarray if (target <= nums[middle]) { right = middle; } else { // For finding the element in the right subarray left = middle + 1; } } // When the target element is greater than the last element of the array, lower_bound is array length if (left < nums.length && nums[left] < target) { left++; } return left; } public static void main(String[] args) { int nums[] = { 2, 3, 5, 7, 12, 15, 17, 20, 32, 54, 67, 98 }; int target = 22; System.out.println("The lower bound index is for target element is " + get_lower_bound(nums, target)); } }
Output
The lower bound index is for target element is 8
Time complexity ? O(logN) for using the binary search algorithm.
Space complexity ? O(1)
Using The Recursive Binary Search For An Alternative Of lower_bound()
In this approach, we will use the recursive binary search algorithm to find the lower bound of the targeted element in the sorted array.
Algorithm
Step 1 ? Initialize the 'left' pointer with 0 and 'right' with the array's length.
Step 2 ? Execute the recursive() function to find the lower bound of the targeted element.
Step 3 ? In the recursive() function, if the 'left' pointer's value is greater than the 'right' pointer's value, return the 'left' value.
Step 4 ? Use the left and right index values to get the middle index.
Step 5 ? If the target element is less than or equal to the element at the middle index, make a recursive call for the left part of the array.
Step 6 - Otherwise, make a recursive call for the right part of the array using the recursive() function call.
Example
import java.util.Arrays; public class Main { static int recursive(int nums[], int left, int right, int target) { if (left > right) { return left; } // Get the middle element int middle = left + (right - left) / 2; // Make a recursive call for the left subarray if (target <= nums[middle]) { return recursive(nums, left, middle - 1, target); } // Make a recursive call for the right subarray return recursive(nums, middle + 1, right, target); } static int get_lower_bound(int nums[], int target) { int left = 0, right = nums.length; return recursive(nums, left, right, target); } public static void main(String[] args) { int nums[] = { 2, 3, 5, 7, 12, 15, 17, 20, 32, 54, 67, 98 }; int target = 22; // Print index of the lower bound System.out.println("The lower bound index is for target element is " + get_lower_bound(nums, target)); } }
Output
The lower bound index is for target element is - 8
Time complexity ? O(logN) for recursive binary search.
Space complexity ? O(1)
The binary search algorithm provides better efficiency than the linear search algorithms in terms of time complexity. However, we can also use the binarySearch() method of the Array utility class to find the lower bound of any element.