Binary Search
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

30:00

Binary Search
easy
Topics
Companies

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

Example 1:
Input: {"nums":[-1,0,3,5,9,12],"target":9}
Output: 4
Constraints:
  • 1nums.length1041 \leq \text{nums.length} \leq 10^4

  • 104<nums[i],target<104-10^4 < \text{nums}[i], \text{target} < 10^4

  • All values in nums are unique.

  • nums is sorted in ascending order.

Input
arr ={"nums":[-1,0,3,5,9,12],"target":9}
Target: 9

-1

L

0

3

5

9

12

R
Variables
No variables to display
DepthFunction Call
Stack empty
0/7