Binary Search Implementation

Problem Implement binary search on a sorted array: return the index of a target value, or -1 if absent.

Input / Output

  • Input: sorted integer array nums (ascending), integer target
  • Output: index of target in nums, or -1

Constraints

  • Array sorted ascending, may contain up to 10^6 elements
  • O(log n) time, O(1) space expected

Example

  • nums = [1, 3, 5, 7, 9, 11], target = 7 → Output: 3
  • nums = [1, 3, 5], target = 4 → Output: -1
asked …
LeaderboardSalaryAccount