Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Binary Search

Easy
binary-searcharray

Given a sorted (ascending) integer array nums of distinct values and an integer target, return the index of target, or -1 if it is not present. Your algorithm must run in O(log n).

Input

  • Line 1: space-separated integers nums (sorted, distinct)
  • Line 2: integer target

Output: the index, or -1.

Example 1
Input:
-1 0 3 5 9 12
9
Output:
4
Example 2
Input:
-1 0 3 5 9 12
2
Output:
-1

Constraints

  • 1 ≤ nums.length ≤ 10^4
  • All values are distinct and sorted ascending.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.