Binary Search is a Divide and Conquer algorithm. Like all divide-and-conquer algorithms, binary search first divides a large array into two smaller subarrays and then recursively (or iteratively) operate the subarrays. But instead of operating on both subarrays, it discards one subarray and continues on the second subarray. This decision of discarding one subarray is made in just one comparison.
So binary search basically reduces the search space to half at each step. By search space, we mean the subarray of a given array where the target value is located (if present in the array). Initially, the search space is the entire array and binary search redefines the search space at every step of the algorithm by using the property of the array that it is sorted. It does so by comparing the mid-value in the search space to the target value. If the target value matches the middle element, its position in the array is returned. Otherwise, it discards half of the search space based on the comparison result.
In this post, we have listed out commonly asked interview questions that use binary search algorithm:
- Binary Search AlgorithmEasy
- Find the number of rotations in a circularly sorted arrayEasy
- Search an element in a circularly sorted arrayMedium
- Find the first or last occurrence of a given number in a sorted arrayEasy
- Count occurrences of a number in a sorted array with duplicatesMedium
- Find the smallest missing element from a sorted arrayMedium
- Find floor and ceil of a number in a sorted integer arrayEasy
- Search in a nearly sorted array in logarithmic timeMedium
- Find the number of 1’s in a sorted binary arrayEasy
- Find the peak element in an arrayMedium
- Find the missing term in a sequence in logarithmic timeMedium
- Find floor and ceil of a number in a sorted array (Recursive solution)Easy
- Find the frequency of each element in a sorted array containing duplicatesEasy
- Find the square root of a number using a binary searchEasy
- Division of two numbers using binary search algorithmMedium
- Find the odd occurring element in an array in logarithmic timeMedium
- Find pairs with difference
kin an array | Constant Space SolutionMedium - Find
kclosest elements to a given value in an arrayMedium - Binary Search in C++ STL and Java CollectionsBeginner
- Ternary Search vs Binary searchBeginner
- Exponential searchEasy
- Unbounded Binary SearchEasy
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)