Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

55. Binary Search

EasyDay 17 of 60Binary Search

You are given an array nums sorted in increasing order with no duplicates, and a value target. Return the index of target in nums, or -1 if it is not there. Your algorithm must run in O(log n) time.

Example 1
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Example 2
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Constraints
  • 1 <= nums.length <= 10^4
  • -10^4 < nums[i], target < 10^4
  • All values are unique
  • `nums` is sorted in ascending order
Hints
Frequently asked at: Microsoft, Amazon, Apple, TCS, Infosys
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min