← Back to problems
UNSW COMP2521 · easy

Binary Search: Index of Target

Binary Search: Index of Target

Given an ascending, distinct integer array and a target, return the 0-based index of the target via binary search, or -1 if absent.

Input: line 1 n; line 2 the n ascending ints; line 3 target. Output: one integer — the index, or -1.

Example: 5 / 2 5 8 12 20 / 8 → 2; target 3 → -1.