AlgoPond
LearnPracticeMockPricing
AlgoPond

Master DSA patterns and ace your next technical interview.

Learn

  • Curriculum
  • Problems
  • Daily Challenge
  • Mock Interview

Account

  • Dashboard
  • Pricing
  • Sign In
  • Get Started

Company

  • Privacy Policy
  • Terms of Service

© 2026 AlgoPond. All rights reserved.

Built for engineers who ship.

easyBinary Search

Binary Search

## Problem

Given a **sorted** array of distinct integers `nums` and a target value, return the index of `target` if it is in the array, or `-1` if it is not.

You must write an algorithm with **O(log n)** runtime complexity.

Examples

Input
nums = [-1,0,3,5,9,12], target = 9
Output
4
9 exists at index 4
Input
nums = [-1,0,3,5,9,12], target = 2
Output
-1
2 does not exist

Constraints

-10^4 <= nums[i], target <= 10^4 1 <= nums.length <= 10^4 nums is sorted in ascending order All integers in nums are unique
Python
Loading...