Lower Bound

Given a non-empty integer array nums sorted in non-decreasing order and an integer target, return the smallest index i such that nums[i] >= target. If no such…

Given a non-empty integer array `nums` sorted in non-decreasing order and an integer `target`, return the smallest index `i` such that `nums[i] >= target`. If no such index exists, return `nums.length`.

- `1 <= nums.length <= 10^4` - `-10^4 <= nums[i], target <= 10^4` - `nums` is sorted in non-decreasing order.

Example 1: input nums = [1,2,4,4,5], target = 4; output 2. Index 2 is the first position whose value is at least 4.

Example 2: input nums = [1,2,4,4,5], target = 3; output 2. Values 1 and 2 are smaller than 3, so index 2 holding 4 is the first value that is at least 3.

Example 3: input nums = [1,2,4,4,5], target = 6; output 5. No array value is at least 6, so no element satisfies nums[i] >= target and the answer is nums.length, which is 5.

Category: Binary Search. Topic: binary-search. Difficulty: Easy. Patterns: binary-search.