Binary Search
Binary Search
AfterAcademy
Admin AfterAcademy
18 Jul 2020
Binary Search
Difficulty: Easy
https://afteracademy.com/blog/binary-search/ 1/9
1/2/25, 12:17 PM Binary Search
Problem Note
Example 1
Example 2
Solution
We need to return the position of an element that needs to be searched in a
sorted array.
https://afteracademy.com/blog/binary-search/ 2/9
1/2/25, 12:17 PM Binary Search
By doing this, the algorithm eliminates the half in which the target value
cannot lie in each iteration.
This searching technique is far more efficient than the linear search. Look
at the example below.
Solution Steps
Pseudo Code
https://afteracademy.com/blog/binary-search/ 3/9
1/2/25, 12:17 PM Binary Search
Complexity Analysis
Complexity Analysis
How did we make sure than if the target < arr[m] , then the target
will exist only on the left of mid?
https://afteracademy.com/blog/binary-search/ 5/9
1/2/25, 12:17 PM Binary Search
comparisons would it take for the binary search to find the name of a
particular species in the array?
Can you list the places where is Binary search algorithm is used?
Search in a 2D Matrix
Dungeon Game
If you have any more approaches or you find an error/bug in the above
solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
https://afteracademy.com/blog/binary-search/ 6/9
1/2/25, 12:17 PM Binary Search
https://afteracademy.com/blog/binary-search/ 7/9
1/2/25, 12:17 PM Binary Search
https://afteracademy.com/blog/binary-search/ 8/9
1/2/25, 12:17 PM Binary Search
https://afteracademy.com/blog/binary-search/ 9/9