Ceiling Position (First ≥ Target)
You are given a list of integers arranged in ascending order and a target value. Your task is to determine the position of the first element in the list whose value is greater than or equal to the target. If all values are strictly smaller than the target, there is no valid position and the correct answer is -1. The result you return is a single index that reflects the earliest place in the list where the target would no longer be considered larger than the element at that position.
Think of the list as milestones on a number line. Starting from the left, values may remain below the target for a while; you are looking for the first point where a value is at least as large as the target. If the target is smaller than or equal to the first element, the answer is index 0. If the target is larger than every element, the answer is -1. Duplicate numbers may appear in the list; when several values are equal to the target, you must return the earliest index among them.
The list is already ordered, so you do not need to modify it. Focus on identifying the boundary where the sequence transitions from values that are less than the target to values that meet or exceed it. The goal is to return the leftmost qualifying index, which captures the moment the list first reaches a value that is not smaller than the target. Report that single index, or -1 when no such position exists.
Example 1:
Input: nums = [1, 2, 4, 4, 7], target = 4
Output: 2
Explanation: The first value that is at least 4 appears at index 2.
Example 2:
Input: nums = [3, 5, 6], target = 7
Output: -1
Explanation: No value is at least 7.
Example 3:
Input: nums = [2, 3, 3, 3, 9], target = 1
Output: 0
Explanation: The first element (2) is already at least 1, so the answer is 0.
Related Problems
No related problems found
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
