0

# Binary Search

Unsolved
###### Search

Difficulty: 3 | Problem written by zeyad_omar
##### Problem reported in interviews at

Binary search is a searching algorithm that is very important and widely used in Computer Science.

Binary search is beautiful due to the fact that it is very fast compared to most searching techniques: complexity O(log(n)).

In this problem you are asked to implement binary search function given a 1D array and a target value.

Return the index of the target value, and if it is not found, return -1.

*Hint: Assume we are searching for the word "engineer" in the dictionary. At first, we open the middle of the dictionary. We find that it is in the "L" section so we know that our word is in the first half of the book. So we search in the middle of the first half. And so on...

**Note: Binary search only works when the input is sorted.

##### Sample Input:
<class 'list'>
x: [1, 2, 3, 4, 5]
<class 'int'>
target: 3

##### Expected Output:
<class 'int'>
2

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Have an issue, the MLPro support team is available 24X7 to Premium users.