Find the Lower Bound of a Target in a Sorted Array

Implement lowerBound

Given an array nums sorted in non-decreasing order (duplicates allowed) and an integer target, return its lower bound — the index of the first element that is greater than or equal to target. If every element is smaller, return nums.length. This is one of the most useful binary-search building blocks: many harder problems (counting occurrences, finding a range, floor/ceil) are just a lower bound and an upper bound combined.

Example 1:

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

Output: 2

Example 2:

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

Output: 2

Example 3:

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

Output: 7

+ 4 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 10⁴
  • -10⁴ ≤ nums[i], target ≤ 10⁴
  • nums is sorted in non-decreasing order (duplicates are allowed)

nums =

[1, 3, 5, 5, 5, 7, 9]

target =

5