Given a sorted array arr[] and a number target, the task is to find the lower bound of the target in this given array. The lower bound of a number is defined as the smallest index in the sorted array where the element is greater than or equal to the given number.
Note: If all the elements in the given array are smaller than the target, the lower bound will be the length of the array.
Examples:
Input: arr[] = {2, 3, 7, 10, 11, 11, 25}, target = 9
Output: 3
Explanation: 3 is the smallest index in arr[] where element (arr[3] = 10) is greater than or equal to 9.
Input: arr[] = {2, 3, 7, 10, 11, 11, 25}, target = 11
Output: 4
Explanation: 4 is the smallest index in arr[] where element (arr[4] = 11) is greater than or equal to 11.
Input: arr[] = {2, 3, 7, 10, 11, 11, 25}, target = 100
Output: 6
Explanation: As no element in arr[] is greater than 100, return the length of array.
[Naive Approach] Using Linear Search - O(n) Time and O(1) Space
The idea is to use linear search. We compare each element of the given array with the target and find the first index where the element is greater than or equal to the target.
C++
// C++ program to find the lower bound of a number
// using linear search
#include <iostream>
#include <vector>
using namespace std;
int lowerBound(vector<int> &arr, int target) {
int n = arr.size();
// compare target with each element in array
for (int i = 0; i < n; ++i) {
// if equal or larger value found,
// return its index
if(arr[i] >= target) {
return i;
}
}
// if all elements are smaller, return length
return n;
}
int main() {
vector<int> arr = {2, 3, 7, 10, 11, 11, 25};
int target = 9;
cout << lowerBound(arr, target);
return 0;
}
C
// C program to find the lower bound of a number
// using linear search
#include <stdio.h>
int lowerBound(int arr[], int n, int target) {
// compare target with each element in array
for (int i = 0; i < n; ++i) {
// if equal or larger value found
// return its index
if(arr[i] >= target) {
return i;
}
}
// if all elements are smaller, return length
return n;
}
int main() {
int arr[] = {2, 3, 7, 10, 11, 11, 25};
int target = 9;
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", lowerBound(arr, n, target));
return 0;
}
Java
// Java program to find the lower bound of a number
// using linear search
import java.util.*;
class GfG {
static int lowerBound(int[] arr, int target) {
int n = arr.length;
// compare target with each element in array
for (int i = 0; i < n; ++i) {
// if equal or larger value found
// return its index
if(arr[i] >= target) {
return i;
}
}
// if all elements are smaller, return length
return n;
}
public static void main(String[] args) {
int[] arr = {2, 3, 7, 10, 11, 11, 25};
int target = 9;
System.out.println(lowerBound(arr, target));
}
}
Python
# Python program to find the lower bound of a number
# using linear search
def lowerBound(arr, target):
n = len(arr)
# compare target with each element in array
for i in range(n):
# if equal or larger value found
# return its index
if arr[i] >= target:
return i
# if all elements are smaller, return length
return n
if __name__ == "__main__":
arr = [2, 3, 7, 10, 11, 11, 25]
target = 9
print(lowerBound(arr, target))
C#
// C# program to find the lower bound of a number
// using linear search
using System;
class GfG {
static int lowerBound(int[] arr, int target) {
int n = arr.Length;
// compare target with each element in array
for (int i = 0; i < n; ++i) {
// if equal or larger value found
// return its index
if (arr[i] >= target) {
return i;
}
}
// if all elements are smaller, return length
return n;
}
static void Main() {
int[] arr = {2, 3, 7, 10, 11, 11, 25};
int target = 9;
Console.WriteLine(lowerBound(arr, target));
}
}
JavaScript
// JavaScript program to find the lower bound of a number
// using linear search
function lowerBound(arr, target) {
let n = arr.length;
// compare target with each element in array
for (let i = 0; i < n; ++i) {
// if equal or larger value found
// return its index
if (arr[i] >= target) {
return i;
}
}
// if all elements are smaller, return length
return n;
}
let arr = [2, 3, 7, 10, 11, 11, 25];
let target = 9;
console.log(lowerBound(arr, target));
[Expected Approach] Using Binary Search - O(log n) Time and O(1) Space
The idea is to use the fact that the given array is sorted. We can apply binary search to find the index of the element greater than or equal to the target.
Step-by-step implementation:
- Set variables lo and hi to the starting and ending of array.
- Find mid = (lo + hi) / 2 and compare arr[mid] with target
- if a[mid] >= target, then lower bound will be in the range arr[lo...mid], so update result to mid and hi to mid - 1.
- if a[mid] < target, then all elements in the range [lo...mid] will also be less than target, so update lo to mid + 1.
- Continue step 2 till lo <= hi.
- Return result as the lower bound.
C++
// C++ program to find the lower bound of a number
// using Binary Search
#include <iostream>
#include <vector>
using namespace std;
int lowerBound(vector<int> &arr, int target) {
int lo = 0, hi = arr.size() - 1;
int res = arr.size();
while(lo <= hi) {
int mid = lo + (hi - lo)/2;
// If arr[mid] >= target, then mid can be the
// lower bound, so update res to mid and
// search in left half, i.e. [lo...mid-1]
if(arr[mid] >= target) {
res = mid;
hi = mid - 1;
}
// If arr[mid] < target, then lower bound
// cannot lie in the range [lo...mid] so
// search in right half, i.e. [mid+1...hi]
else {
lo = mid + 1;
}
}
return res;
}
int main() {
vector<int> arr = {2, 3, 7, 10, 11, 11, 25};
int target = 9;
cout << lowerBound(arr, target);
return 0;
}
C
// C program to find the lower bound of a number
// using Binary Search
#include <stdio.h>
int lowerBound(int* arr, int n, int target) {
int lo = 0, hi = n - 1;
int res = n;
while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
// If arr[mid] >= target, then mid can be the
// lower bound, so update res to mid and
// search in left half, i.e. [lo...mid-1]
if(arr[mid] >= target) {
res = mid;
hi = mid - 1;
}
// If arr[mid] < target, then lower bound
// cannot lie in the range [lo...mid] so
// search in right half, i.e. [mid+1...hi]
else {
lo = mid + 1;
}
}
return res;
}
int main() {
int arr[] = {2, 3, 7, 10, 11, 11, 25};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 9;
printf("%d\n", lowerBound(arr, n, target));
return 0;
}
Java
// Java program to find the lower bound of a number
// using Binary Search
class GfG {
static int lowerBound(int[] arr, int target) {
int lo = 0, hi = arr.length - 1;
int res = arr.length;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
// If arr[mid] >= target, then mid can be the
// lower bound, so update res to mid and
// search in left half, i.e. [lo...mid-1]
if (arr[mid] >= target) {
res = mid;
hi = mid - 1;
}
// If arr[mid] < target, then lower bound
// cannot lie in the range [lo...mid] so
// search in right half, i.e. [mid+1...hi]
else {
lo = mid + 1;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {2, 3, 7, 10, 11, 11, 25};
int target = 9;
System.out.println(lowerBound(arr, target));
}
}
Python
# Python program to find the lower bound of a number
# using Binary Search
def lowerBound(arr, target):
lo = 0
hi = len(arr) - 1
res = len(arr)
while lo <= hi:
mid = lo + (hi - lo) // 2
# If arr[mid] >= target, then mid can be the
# lower bound, so update res to mid and
# search in left half, i.e. [lo...mid-1]
if arr[mid] >= target:
res = mid
hi = mid - 1
# If arr[mid] < target, then lower bound
# cannot lie in the range [lo...mid] so
# search in right half, i.e. [mid+1...hi]
else:
lo = mid + 1
return res
if __name__ == "__main__":
arr = [2, 3, 7, 10, 11, 11, 25]
target = 9
print(lowerBound(arr, target))
C#
// C# program to find the lower bound of a number
// using Binary Search
using System;
class GfG {
static int LowerBound(int[] arr, int target) {
int lo = 0, hi = arr.Length - 1;
int res = arr.Length;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
// If arr[mid] >= target, then mid can be the
// lower bound, so update res to mid and
// search in left half, i.e. [lo...mid-1]
if (arr[mid] >= target) {
res = mid;
hi = mid - 1;
}
// If arr[mid] < target, then lower bound
// cannot lie in the range [lo...mid] so
// search in right half, i.e. [mid+1...hi]
else
lo = mid + 1;
}
return res;
}
static void Main() {
int[] arr = { 2, 3, 7, 10, 11, 11, 25 };
int target = 9;
Console.WriteLine(LowerBound(arr, target));
}
}
JavaScript
// JavaScript program to find the lower bound of a number
// using Binary Search
function lowerBound(arr, target) {
let lo = 0, hi = arr.length - 1;
let res = arr.length;
while (lo <= hi) {
let mid = lo + Math.floor((hi - lo) / 2);
// If arr[mid] >= target, then mid can be the
// lower bound, so update res to mid and
// search in left half, i.e. [lo...mid-1]
if (arr[mid] >= target) {
res = mid;
hi = mid - 1;
}
// If arr[mid] < target, then lower bound
// cannot lie in the range [lo...mid] so
// search in right half, i.e. [mid+1...hi]
else {
lo = mid + 1;
}
}
return res;
}
const arr = [2, 3, 7, 10, 11, 11, 25];
const target = 9;
console.log(lowerBound(arr, target));
Related Articles:
Similar Reads
Implement Upper Bound Given a sorted array arr[] and a number target, the task is to find the upper bound of the target in this given array. The upper bound of a number is defined as the smallest index in the sorted array where the element is greater than the given number. Note: If all the elements in the given array are
10 min read
Lower and Upper Bound Theory Lower and upper bound theory is a mathematical concept that involves finding the smallest and largest possible values for a quantity, given certain constraints or conditions. It is often used in optimization problems, where the goal is to find the maximum or minimum value of a function subject to ce
15+ min read
Implementation of lower_bound() and upper_bound() in List of Pairs in C++ In this article, we will discuss the implementation of the lower_bound() and upper_bound() in a list of pairs. lower_bound(): It returns an iterator pointing to the first element in the range [first, last) which has a value greater than or equals to the given value âvalâ. But in List of Pairs lower_
3 min read
Implementation of 0/1 Knapsack using Branch and Bound Given two arrays v[] and w[] that represent values and weights associated with n items respectively. Find out the maximum value subset(Maximum Profit) of v[] such that the sum of the weights of this subset is smaller than or equal to Knapsack capacity Cap(W). Note: The constraint here is we can eith
15+ min read
Sudo Placement | Range Queries Given Q queries, with each query consisting of two integers L and R, the task is to find the total numbers between L and R (Both inclusive), having almost three set bits in their binary representation. Examples: Input : Q = 2 L = 3, R = 7 L = 10, R = 16 Output : 5 6 For the first query, valid number
13 min read