Asked at
Binary Search
EasyVerifiedArrayBinary Search~15 min
Given a sorted array of integers nums (ascending) and an integer target, return the index of target if it is present, otherwise return -1.
Your algorithm must run in O(log n) time. The input arrives as a single object { nums, target }.
Examples
in{ nums: [-1, 0, 3, 5, 9, 12], target: 9 }
out4
9 sits at index 4.
in{ nums: [-1, 0, 3, 5, 9, 12], target: 2 }
out-1
2 is not in nums.
Constraints
- 1 ≤ nums.length ≤ 10⁴
- nums is sorted in ascending order.
- All integers in nums are unique.
- Target: O(log n) time.
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.