Java Equivalent of C++’s upper_bound() Method
Last Updated :
28 Jan, 2022
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. The below implementations will find the upper bound value and its index, otherwise, it will print upper bound does not exist.
Illustrations:
Input : 10 20 30 30 40 50
Output : upper_bound for element 30 is 40 at index 4
Input : 10 20 30 40 50
Output : upper_bound for element 45 is 50 at index 4
Input : 10 20 30 40 50
Output : upper_bound for element 60 does not exists
Now let us discuss out the methods in order to use the upper bound() method in order to get the index of the next greater value.
Methods:
- Naive Approach (linear search)
- Iterative binary search
- Recursive binary search
- binarySearch() method of Arrays utility class
Let us discuss each of the above methods to detailed understanding by providing clean java programs for them as follows:
Method 1: Using linear search
To find the upper bound using linear search, we will iterate over the array starting from the 0th index until we find a value greater than the key.
Example
Java
import java.util.Arrays;
class GFG {
static void upper_bound( int arr[], int key)
{
int upperBound = 0 ;
while (upperBound < arr.length) {
if (arr[upperBound] <= key)
upperBound++;
else {
System.out.print( "The upper bound of " + key + " is " + arr[upperBound] + " at index " + upperBound);
return ;
}
}
System.out.print( "The upper bound of " + key + " does not exist." );
}
public static void main(String[] args)
{
int array[] = { 10 , 20 , 30 , 30 , 40 , 50 };
int key = 30 ;
Arrays.sort(array);
upper_bound(array, key);
}
}
|
Output
The upper bound of 30 is 40 at index 4
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(1)
To find the upper bound of a key, we will search the key in the array. 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 examples.
Method 2: Using binary search iteratively
Procedure:
- Sort the array before applying binary search.
- Initialize low as 0 and high as N.
- Find the index of the middle element (mid)
- Compare key with the middle element(arr[mid])
- If the middle element is less than or equals to key then update the low as mid+1, Else update high as mid.
- Repeat step 2 to step 4 until low is less than high.
- After all the above steps the low is the upper_bound of the key
Example
Java
import java.util.Arrays;
public class GFG {
static void upper_bound( int arr[], int key)
{
int mid, N = arr.length;
int low = 0 ;
int high = N;
while (low < high && low != N) {
mid = low + (high - low) / 2 ;
if (key >= arr[mid]) {
low = mid + 1 ;
}
else {
high = mid;
}
}
if (low == N ) {
System.out.print( "The upper bound of " + key + " does not exist." );
return ;
}
System.out.print( "The upper bound of " + key + " is " + arr[low] + " at index " + low);
}
public static void main(String[] args)
{
int array[] = { 10 , 20 , 30 , 30 , 40 , 50 };
int key = 30 ;
Arrays.sort(array);
upper_bound(array, key);
}
}
|
Output
The upper bound of 30 is 40 at index 4
A recursive approach following the same procedure is discussed below:
Method 3: Recursive binary search
Example
Java
import java.util.Arrays;
public class GFG {
static int recursive_upper_bound( int arr[], int low,
int high, int key)
{
if (low > high || low == arr.length)
return low;
int mid = low + (high - low) / 2 ;
if (key >= arr[mid]) {
return recursive_upper_bound(arr, mid + 1 , high,
key);
}
return recursive_upper_bound(arr, low, mid - 1 ,
key);
}
static void upper_bound( int arr[], int key)
{
int low = 0 ;
int high = arr.length;
int upperBound
= recursive_upper_bound(arr, low, high, key);
if (upperBound == arr.length)
System.out.print( "The upper bound of " + key
+ " does not exist." );
else System.out.print(
"The upper bound of " + key + " is "
+ arr[upperBound] + " at index "
+ upperBound);
}
public static void main(String[] args)
{
int array[] = { 10 , 20 , 30 , 30 , 40 , 50 };
int key = 30 ;
Arrays.sort(array);
upper_bound(array, key);
}
}
|
Output
The upper bound of 30 is 40 at index 4
Method 4: Using binarySearch() method of Arrays utility class
We can also use the in-built binary search method of Arrays utility class (or Collections utility class). This function returns an index of the key in the array. If the key is present in the array it will return its index (not guaranteed to be the first index), otherwise based on sorted order, it will return the expected position of the key i.e (-(insertion point) – 1).
Approach:
- Sort the array before applying binary search.
- Search the index of the key in a sorted array, if it is present in the array, its index is returned as positive value of , otherwise, a negative value which specifies the position at which the key should be added in the sorted array.
- Now if the key is present in the array we move rightwards to find next greater value.
- Print the upper bound index if present.
Example
Java
import java.util.Arrays;
public class GFG {
static void upper_bound( int arr[], int key)
{
int index = Arrays.binarySearch(arr, key);
int n = arr.length;
if (index < 0 ) {
int upperBound = Math.abs(index) - 1 ;
if (upperBound < n)
System.out.print( "The upper bound of " + key
+ " is " + arr[upperBound]
+ " at index "
+ upperBound);
else
System.out.print( "The upper bound of " + key
+ " does not exists." );
return ;
}
else {
while (index < n) {
if (arr[index] == key)
index++;
else {
System.out.print(
"The upper bound of " + key + " is "
+ arr[index] + " at index "
+ index);
return ;
}
}
System.out.print( "The upper bound of " + key
+ " does not exist." );
}
}
public static void main(String[] args)
{
int array[] = { 10 , 20 , 30 , 30 , 40 , 50 };
int key = 30 ;
Arrays.sort(array);
upper_bound(array, key);
}
}
|
Output
The upper bound of 30 is 40 at index 4
Note: We can also find the index of the middle element via any one of them
int mid = (high + low)/ 2;
int mid = (low + high) >>> 1;
Share your thoughts in the comments
Please Login to comment...