Upper Bound

Last Updated : 30 Oct, 2026

Given a sorted array arr[] and a number target, 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 smaller than or equal to the target, the upper 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[] at which element (arr[3] = 10) is larger than 9.

Input: arr[] = [2, 3, 7, 10, 11, 11, 25], target = 11
Output: 6
Explanation: 6 is the smallest index in arr[]at which element (arr[6] = 25) is larger than 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.

Try It Yourself
redirect icon

[Naive Approach] 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 target.

C++
#include <iostream>
#include <vector>
using namespace std;

int upperBound(vector<int> &arr, int target) {
    int n = arr.size();
	
  	// Compare target with each element in array
    for (int i = 0; i < n; ++i) {
      
      	// If 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 = 11;
  
    cout << upperBound(arr, target);
    return 0;
}
C
#include <stdio.h>

int upperBound(int arr[], int n, int target) {

    // Compare target with each element in array
    for (int i = 0; i < n; ++i) {

        // If 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 = 11;
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d", upperBound(arr, n, target));
    return 0;
}
Java
class GfG {

    // Function to find the upper bound of a number
    static int upperBound(int[] arr, int target) {
        int n = arr.length;

        // Compare target with each element in array
        for (int i = 0; i < n; ++i) {

            // If 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 = 11;

        System.out.println(upperBound(arr, target));
    }
}
Python
def upperBound(arr, target):
    n = len(arr)

    # Compare target with each element in array
    for i in range(n):

        # If 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 = 11
    print(upperBound(arr, target))
C#
using System;

class GfG {
  
    static int upperBound(int[] arr, int target) {
        int n = arr.Length;

        // Compare target with each element in array
        for (int i = 0; i < n; ++i) {

            // If 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 = 11;

        Console.WriteLine(upperBound(arr, target));
    }
}
JavaScript
function upperBound(arr, target) {
    let n = arr.length;

    // Compare target with each element in array
    for (let i = 0; i < n; ++i) {

        // If larger value found, return its index
        if (arr[i] > target) {
            return i;
        }
    }

    // If all elements are smaller, return length
    return n;
}

// Driver Code
let arr = [2, 3, 7, 10, 11, 11, 25];
let target = 11;
console.log(upperBound(arr, target));

Output
6

[Expected Approach - 1] 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 just larger than the target.

  • Initialize lo = 0, hi = arr.length - 1, and result = arr.length.
  • Calculate the middle index using mid = lo + (hi - lo) / 2 and compare arr[mid] with target.
  • If arr[mid] <= target, update lo = mid + 1 to search the right half. Otherwise, store mid in result and update hi = mid - 1 to search the left half.
  • Repeat until lo > hi.
  • Return result as the upper bound index. If no element is greater than the target, result remains equal to the array length.
C++
#include <iostream>
#include <vector>
using namespace std;

int upperBound(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 arr[mid] can be
        // the upper bound so store mid in result and
        // search in left half, i.e. arr[lo...mid-1]
      	if(arr[mid] > target) {
          	res = mid;
            hi = mid - 1;
        }
        
        // If arr[mid] <= target, then upper bound 
        // cannot lie in the range [lo...mid], so 
        // search in right half, i.e. arr[mid+1...hi]
      	else
            lo = mid + 1;
    }
  	return res;
}

int main() {
    vector<int> arr = {2, 3, 7, 10, 11, 11, 25};
    int target = 11;
  	
    cout << upperBound(arr, target);
    return 0;
}
C
#include <stdio.h>

int upperBound(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 arr[mid] can be
        // the upper bound so store mid in result and
        // search in left half, i.e. arr[lo...mid-1]
        if (arr[mid] > target) {
            res = mid;
            hi = mid - 1;
        }

        // If arr[mid] <= target, then upper bound 
        // cannot lie in the range [lo...mid], so 
        // search in right half, i.e. arr[mid+1...hi]
        else {
            lo = mid + 1;
        }
    }
    return res;
}

int main() {
    int arr[] = {2, 3, 7, 10, 11, 11, 25};
    int target = 11;
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d", upperBound(arr, n, target));
    return 0;
}
Java
class GfG {

    static int upperBound(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 arr[mid] can be
            // the upper bound so store mid in result and
            // search in left half, i.e. arr[lo...mid-1]
            if (arr[mid] > target) {
                res = mid;
                hi = mid - 1;
            }

            // If arr[mid] <= target, then upper bound 
            // cannot lie in the range [lo...mid], so 
            // search in right half, i.e. arr[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 = 11;

        System.out.println(upperBound(arr, target));
    }
}
Python
def upperBound(arr, target):
    lo, hi = 0, len(arr) - 1
    res = len(arr)
    
    while lo <= hi:
        mid = lo + (hi - lo) // 2
        
        # If arr[mid] > target, then arr[mid] can be
        # the upper bound so store mid in result and
        # search in left half, i.e. arr[lo...mid-1]
        if arr[mid] > target:
            res = mid
            hi = mid - 1
        
        # If arr[mid] <= target, then upper bound 
        # cannot lie in the range [lo...mid], so 
        # search in right half, i.e. arr[mid+1...hi]
        else:
            lo = mid + 1
    
    return res


if __name__ == "__main__":
    arr = [2, 3, 7, 10, 11, 11, 25]
    target = 11
    
    print(upperBound(arr, target))
C#
using System;

class GfG {
    static int upperBound(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 arr[mid] can be
            // the upper bound so store mid in result and
            // search in left half, i.e. arr[lo...mid-1]
            if (arr[mid] > target) {
                res = mid;
                hi = mid - 1;
            }
            
            // If arr[mid] <= target, then upper bound 
            // cannot lie in the range [lo...mid], so 
            // search in right half, i.e. arr[mid+1...hi]
            else {
                lo = mid + 1;
            }
        }
        
        return res;
    }

    static void Main(string[] args) {
        int[] arr = { 2, 3, 7, 10, 11, 11, 25 };
        int target = 11;
        
        Console.WriteLine(upperBound(arr, target));
    }
}
JavaScript
function upperBound(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 arr[mid] can be
        // the upper bound so store mid in result and
        // search in left half, i.e. arr[lo...mid-1]
        if (arr[mid] > target) {
            res = mid;
            hi = mid - 1;
        }
        
        // If arr[mid] < target, then upper bound 
        // cannot lie in the range [lo...mid], so 
        // search in right half, i.e. arr[mid+1...hi]
        else {
            lo = mid + 1;
        }
    }
    return res;
}

// Driver Code
let arr = [2, 3, 7, 10, 11, 11, 25];
let target = 11;

console.log(upperBound(arr, target));

Output
6

[Expected Approach - 2] Built-In Methods - O(log n) Time and O(1) Space

We can use built-in functions to find the Upper bound of an element in a sorted array efficiently.

  • C++: std::upper_bound(v.begin(), v.end(), x) (from <algorithm>)
  • Python: bisect.bisect_right(arr, x) (from bisect module)
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int upperBound(vector<int> &arr, int target) {
    
   // Using inbuilt method
   int index = upper_bound(arr.begin(), arr.end(), 
                                    target) - arr.begin();
  
   return index;
}

int main() {
    vector<int> arr = {2, 3, 7, 10, 11, 11, 25};
    int target = 11;
  
    cout << upperBound(arr, target);
    return 0;
}
Python
import bisect

def upperBound(arr, target):
    
    # Using bisect_right to get the upper bound index
    index = bisect.bisect_right(arr, target)
    return index

if __name__ == "__main__":
    arr = [2, 3, 7, 10, 11, 11, 25]
    target = 11
    
    print(upperBound(arr, target))

Output
6

Related Articles:

Comment