Given a sorted array arr[] and a number target, 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 target.
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: 7
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++
#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
#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
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
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#
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
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 - 1] 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 arr[mid] >= target, then lower bound will be in the range arr[lo...mid], so update result to mid and hi to mid - 1.
=> if arr[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++
#include <iostream>
#include <vector>
using namespace std;
int lowerBound(vector<int> &arr, int target) {
int low = 0, high = arr.size() - 1;
int res = arr.size();
while(low <= high) {
int mid = low + (high - low)/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;
high = 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 {
low = 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
#include <stdio.h>
int lowerBound(int* arr, int n, int target) {
int low = 0, high = n - 1;
int res = n;
while(low <= high) {
int mid = low + (high - low) / 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;
high = 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 {
low = 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
class GfG {
static int lowerBound(int[] arr, int target) {
int low = 0, high = arr.length - 1;
int res = arr.length;
while (low <= high) {
int mid = low + (high - low) / 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;
high = 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 {
low = 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
def lowerBound(arr, target):
low = 0
high = len(arr) - 1
res = len(arr)
while low <= high:
mid = low + (high - low) // 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
high = 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:
low = mid + 1
return res
if __name__ == "__main__":
arr = [2, 3, 7, 10, 11, 11, 25]
target = 9
print(lowerBound(arr, target))
C#
using System;
class GfG {
static int LowerBound(int[] arr, int target) {
int low = 0, high = arr.Length - 1;
int res = arr.Length;
while (low <= high) {
int mid = low + (high - low) / 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;
high = 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
low = 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
function lowerBound(arr, target) {
let low = 0, high = arr.length - 1;
let res = arr.length;
while (low <= high) {
let mid = low + Math.floor((high - low) / 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;
high = 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 {
low = mid + 1;
}
}
return res;
}
// Driver Code
const arr = [2, 3, 7, 10, 11, 11, 25];
const target = 9;
console.log(lowerBound(arr, target));
[Expected Approach - 2] Using Built-In Methods - O(log n) Time and O(1) Space
We can use built-in functions to find the lower bound of an element in a sorted array efficiently.
- C++: std::lower_bound(v.begin(), v.end(), x) (from <algorithm>)
- Python: bisect.bisect_left(arr, x) (from bisect module)
For more details see lower_bound in c++, bisect in python
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int lowerBound(vector<int> &arr, int target) {
// Using inbuilt method
int index = lower_bound(arr.begin(), arr.end(),
target) - arr.begin();
return index;
}
int main() {
vector<int> arr = {2, 3, 7, 10, 11, 11, 25};
int target = 9;
cout << lowerBound(arr, target);
return 0;
}
Python
import bisect
def lowerBound(arr, target):
# Using bisect_left to get the lower bound index
index = bisect.bisect_left(arr, target)
return index
if __name__ == "__main__":
arr = [2, 3, 7, 10, 11, 11, 25]
target = 9
print(lowerBound(arr, target))
Related Articles:
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem