![]() |
VOOZH | about |
Binary Search is widely recognized as one of the most efficient search algorithms, with numerous applications in programming for its speed and precision.
However, many problems often lack explicit hints about applying Binary Search or even using a searching algorithm at all. Therefore, itβs essential to learn how to recognize when Binary Search can be used, how to approach and solve Binary Search problems, and to be aware of the most common interview Binary Search questions that appear in technical interviews. This post aims to guide you through identifying and solving questions on Binary Search by covering essential topics that will help you master this powerful technique.
Table of Content
Binary search is a searching technique to search an ordered list of data based on the Divide and Conquer technique which repeatedly divides the search space by half in every iteration.
Here are four types of problems where Binary Search can or cannot be applied:
In this case, Identification as a binary search problem is very straightforward as we can easily deduce which half needs to be removed from the search space.
In this case also binary search can be applied as the problem follows a monotonic nature which means the function can be either increasing or decreasing with an increase in a parameter so that one-half can be removed from search space.
Whenever we can identify that the answer to the problem lies between in a range L to R and there is a Monotonic Behaviour of the answer in range L to R then we can think to apply binary search on the answer.
In this case we can not apply binary search.
Now here is the general technique to Identify and Solve the Binary search problems.
Based on the number of problems involving Binary Search, we can safely assume the scenario when Binary search is involved in a problem, as mentioned below:
Binary search can only be applied to a problem if and only if the Problem is monotonic in nature.
Suppose we are given a problem f() and the expected solution to this problem is y, then we can define the problem as
y = f(n)Now if this problem show a behaviour such that it either only increases or only decreases with the change in parameter. Then it is said that the problem shows Monotonic (only single direction) behaviour.
In other words, A function f(n1) is said to be monotonic if and only if:
- for any n1, iff(n1)returns true, then for any value of n2 (where n2 > n1), f(n2) should also return true,
- and similarly, if for a certain value of n1 for whichf(n1)is false, then for any value n2 (n2 < n1) the function f(n2) should also return false.
The same is shown in the graph below:
Now if this problem shows a monotonic behavior, generally this tells you that you can apply a binary search here.
Input: arr[] = {1, 4, 45, 6, 0, 19}, x = 51
Output: 3
Explanation:Minimum length subarray is {4, 45, 6}
Let n1 is size of subarray and f(n1) is true i.e. there is a subarray of size n1 whose sum is greater than x.
Take any n2 (n2>n1) i.e at least one more element than previous subarray size then it is guaranteed that f(n2) must hold true because there is subarray of size n1 has sum greater than x, adding at least one non-negative integer increases the sum.
Problem is monotonic in nature and can be solved using binary search.
Implementation of the above solution:
3
Time Complexity:O(NlogN),N is the size of the subarray
Auxiliary space: O(1)
Problem Name | Practice Link to the Problem |
|---|---|
Binary Search | |
Floor in a Sorted Array | |
First and last occurrences of X | |
Peak element | |
Square root of a number | |
Koko Eating Bananas | |
Minimum days to make M bouquets | |
Smallest Divisor | |
Capacity To Ship Packages Within D Days | |
Aggressive Cows | |
Allocate minimum number of pages | |
Median of 2 Sorted Arrays of Different Sizes |