Problem
You get an array of integers nums and an integer target. The items of nums are in ascending order, and no two items are equal.
Return the index of target in nums. If target is not in nums, return -1. Your solution must run in O(log n) time.
Example 1
- Input:
- nums = [-4,0,3,8,15,21], target = 8
- Output:
- 3
- Explanation:
nums[3]is 8.
Example 2
- Input:
- nums = [2,6,9], target = 5
- Output:
- -1
- Explanation:
- The value 5 is not in
nums.
Example 3
- Input:
- nums = [7], target = 7
- Output:
- 0
Constraints
- 1 <= nums.length <= 10^4
- -10^4 < nums[i], target < 10^4
- All the integers in nums are unique.
- nums is in ascending order.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Binary Searchable Numbers in an Unsorted Array
Coding · Array · Binary Search · Monotonic Stack
- Search a 2D Matrix
Coding · Array · Binary Search · Matrix
- Search a 2D Matrix II
Coding · Array · Binary Search · Divide and Conquer
- Search in Rotated Sorted Array
Coding · Array · Binary Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 4 records
Amazon
Leetcode 704. Binary Search
Reported Oct 31, 2024 · 1 source report
JUNIOR
Source snapshot: Nov 12, 2026
View sourceGoogle
Leetcode 704. Binary Search
Reported Sep 3, 2024 · 1 source report
INTERN
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 704. Binary Search
Reported Nov 14, 2025 · 2 source reports
MANAGER, SENIOR
Source snapshot: Nov 12, 2026
View sourceVisa
Leetcode 704. Binary Search
Reported Jul 27, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source