Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Lower Bound

Medium Acceptance 75.00% Points 30.00

Given a sorted array of integers and a target value, return the index of the first element in the array that is greater than or equal to 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: 2

Explanation: The element 5 is present at index 2, which is the first element greater than or equal to the target.

Example 2

Example 2

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

Output: 3

Explanation: The first element greater than or equal to 6 is 7, which is at index 3.

Example 3

Example 3

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

Output: 4

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

Hints
Hint 1
N/A
Constraints
  • 1 = arr.length = 105
  • -104 = arr[i], target = 104
  • arr is sorted in non-decreasing order
Companies
Samsung Google Uber Spotify
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions