easyBinary SearchPure DSA~11 min
Find Target Build Index
A CI dashboard stores build numbers sorted ascending. Given a specific build number, locate its index for a deep link — or report that it was pruned from history.
Problem
Given a sorted array nums and a target, return the index of target in nums, or -1 if it is not present.
Input
A sorted array nums of length n (0 ≤ n ≤ 10^5) and integer target.
Output
A single integer — the index of target, or -1.
Constraints
- nums is sorted ascending with distinct values
- 0 ≤ n ≤ 100,000
- Target O(log n)
Examples
Example 1
Input
nums = [-1, 0, 3, 5, 9, 12], target = 9
Output
4
9 sits at index 4.
Example 2
Input
nums = [-1, 0, 3, 5, 9, 12], target = 2
Output
-1
2 is absent from the array.