Binary search · Easy
An ordered index may contain many copies of the same event code. A new record must be placed after every existing equal code. The caller supplies the sorted values and needs only the insertion boundary, without changing the index.
Return the first index whose value is strictly greater than the target code.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[], target: integer }
Fields
- nums is already sorted in nondecreasing numeric order. Do not sort it again.
- target is an integer between -2000000000 and 2000000000.
Output
Return value
- Return a zero-based index from 0 through nums.length. Return nums.length when no value is greater.
- Every value equal to target must remain before the returned boundary. Empty input returns 0.
Constraints
- 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
- All totals and counts fit exactly in a safe JSON integer.
Examples
Example 1
{
"nums": [
1,
2,
2,
4
],
"target": 2
}Returns
3Example 2
{
"nums": [
1,
2
],
"target": 9
}Returns
2Required complexity
O(log(n + 1)) search time and O(1) auxiliary space