Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Upper Bound

Easy Acceptance 80% Points 20

Given a sorted array of integers and a target value, return the index of the first element in the array that is strictly greater than the target. If no such element exists, return the length of the array. The array is sorted in non-decreasing order.

Examples
Example 1

Example 1

Input: arr = [1,3,5,7,9], target = 5

Output: 3

Explanation: The first element greater than 5 is 7, which is at index 3.

Example 2

Example 2

Input: arr = [1,2,2,2,4,6], target = 2

Output: 4

Explanation: The first element greater than 2 is 4, which is at index 4.

Example 3

Example 3

Input: arr = [2,4,6,8], target = 10

Output: 4

Explanation: No element is greater than 10, so return the length of the array, which is 4.

Constraints
  • 1 = arr.length = 105
  • -104 = arr[i], target = 104
  • arr is sorted in non-decreasing order
Topics
Binary Search
Companies
Microsoft
Added 19 Jul 2026 80% acceptance
Ln 1, Col 1Sign in to save your code
1
1
1