All Problems
mediumFunctions

Binary Search

Problem Statement
Implement binary search. Given a sorted array and a target, find the index of the target. Print -1 if not found.

SAMPLE INPUT

6
1 3 5 7 9 11
7

SAMPLE OUTPUT

3

CONSTRAINTS

  • 1 <= n <= 100000
  • Array is sorted in ascending order

Explanation: Repeatedly divide the search space in half.

Code Editor
Output
Click "Run" to see output...