Binary Search
easy⏱ 15 mintypescriptchallengesproblem-solving-basic-
Sample case — your program reads stdin and prints to stdout.
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return -1.
Binary search works by repeatedly dividing the search interval in half.
Example: nums=[−1,0,3,5,9,12], target=9 → index 4
Input format
Line 1: n (array size). Line 2: n space-separated sorted integers. Line 3: target.
Output format
The index of target (0-based), or -1 if not found.
Constraints
1 ≤ n ≤ 10⁴, -10⁴ ≤ nums[i], target ≤ 10⁴