BudiBadu Logo

Ceiling Position (First ≥ Target)

Array Easy 8 views
Like12

You’re given a list of integers in ascending order and a target value. Your mission in Ceiling Position is to find the index of the first element that is greater than or equal to that target. If every number in the list is smaller than your target, there is no valid position, and you should return -1.

The "secret sauce" here is Binary Search. Since the list is already sorted, you don''t need to check every number one by one. Instead, you can repeatedly split the list in half to find the boundary where values transition from "smaller than target" to "at least target." This approach is incredibly fast, even for huge datasets! You must return the leftmost qualifying index, meaning if the target appears multiple times, you pick the very first one. This simple lookup is a fundamental building block for range-based queries and data filtering!

If sorting is part of the strategy, do it intentionally as a preprocessing step to simplify downstream logic such as merging, ordering, or comparison. After sorting, keep output semantics precise: preserve expected structure, avoid dropping valid entries, and ensure tied cases still follow deterministic order rules.

Examples

Example 1
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.

Example 2
Input
nums = [3, 5, 6], target = 7
Output
-1
Explanation

No value is at least 7.

Example 3
Input
nums = [1, 2, 4, 4, 7], target = 4
Output
2
Explanation

The first value that is at least 4 appears at index 2.

Algorithm Flow

Recommendation Algorithm Flow for Ceiling Position (First ≥ Target) - Budibadu
Recommendation Algorithm Flow for Ceiling Position (First ≥ Target) - Budibadu

Best Answers

java
class Solution {
    public int ceiling_position(Object nums, Object target) {
        int[] arr = (int[]) nums; int t = (int) target;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= t) return i;
        }
        return -1;
    }
}