Find Leftmost Position Meeting Threshold
Heaps, Sorting & SearchAuto-graded · 22 test casesLast updated
Tests understanding of lower-bound binary search with correct boundary handling and duplicate treatment.
Write a function that locates the leftmost position in a sorted array where a value meets or exceeds a threshold.
You're given a sorted array nums of integers and a threshold value target. Return the index of the first element that is greater than or equal to target. If no element meets this condition, return -1.
Your solution must handle arrays containing duplicate values correctly, returning the leftmost qualifying position.
Constraints
0 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9-10^9 <= target <= 10^9numsis sorted in non-decreasing order- Your solution should run in O(log n) time
Examples
Example 1
Input: find_threshold_position([1, 3, 5, 7], 5)
Output: 2
Example 2
Input: find_threshold_position([1, 3, 5, 7], 4)
Output: 2
Example 3
Input: find_threshold_position([1, 3, 5, 7], 10)
Output: -1
Example 4
Input: find_threshold_position([1, 1, 2, 2, 4], 2)
Output: 2
Hints
Hint 1
Think about what property divides the array into two regions: elements below the threshold and elements at or above it.
Hint 2
Use a half-open interval [lo, hi) where hi starts at n (not n-1) to naturally handle the 'not found' case.
Hint 3
When you find an element >= target, don't return immediately—it might not be the leftmost one. Keep searching to the left.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires finding the lower bound in a sorted array: the leftmost position where the condition nums[i] >= target becomes true.
The key insight is recognizing that the sorted array creates a monotonic pattern. Once you encounter an element meeting the threshold, all subsequent elements also meet it due to the sorted property. This creates two regions:
[elements < target] [elements >= target]
^ we want this boundary
We use binary search with a half-open interval [lo, hi) where lo = 0 and hi = len(nums). Starting hi at the length (not len-1) elegantly handles the case where no qualifying element exists.
During each iteration:
- Calculate
mid = (lo + hi) // 2 - If
nums[mid] >= target: this position qualifies, but there might be an earlier one. Movehi = midto search left, keepingmidas a candidate. - If
nums[mid] < target: this position and everything before it are too small. Movelo = mid + 1to search right.
The search maintains the invariant that all indices before lo fail the condition and all indices from hi onward satisfy it. When lo == hi, we've found the exact boundary.
For duplicates, we never return early upon finding a match. By always setting hi = mid when the condition holds, we continue narrowing leftward until we reach the first occurrence.
Why It Works
The loop preserves two guarantees:
- Every index
< lohasnums[i] < target - Every index
>= hihasnums[i] >= target
These hold initially (vacuously true) and after each update. When the loop exits with lo == hi, this value represents the transition point: the first index where the condition becomes true, or len(nums) if no such element exists.
Complexity
Time: O(log n) - each iteration halves the search space
Space: O(1) - only a constant number of variables used
The iterative version is preferred in production. A recursive implementation would use O(log n) stack space but produce identical results.
Solution
from typing import List
def find_threshold_position(nums: List[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] >= target:
hi = mid
else:
lo = mid + 1
return lo if lo < len(nums) else -1