Example 1 · empty
- Arguments
- values
[]
target0
- Expected return
-1
Test data
- Input
{"values":[],"target":0}- Expected output
-1
func FirstTargetIndex(values []int, target int) intExamples use {"values": [...], "target": ...}.
The runner calls your functions directly; you do not need to parse JSON.
[]
0
-1
{"values":[],"target":0}-1
[ 1, 2, 2, 2, 5 ]
2
1
{"values":[1,2,2,2,5],"target":2}1
[ 1, 3, 7 ]
4
-1
{"values":[1,3,7],"target":4}-1
[ -4, 0, 8 ]
8
2
{"values":[-4,0,8],"target":8}2
Every discarded position before left is strictly smaller than target, while narrowing the right boundary on equality preserves the possibility of an earlier match. On convergence, no matching position can precede left. The final bounds/equality check distinguishes an actual match from the insertion position for an absent target.
O(log n) time for nonempty input and O(1) auxiliary space. Empty input is handled in O(1). The input remains unchanged.
Checking execution availability…
View inputs and expected outputs. Examples do not run your code.