![]() |
VOOZH | about |
Binary Search on Answer is the algorithm in which we are finding our answer with the help of some particular conditions. We have given a search space in which we take an element [mid] and check its validity as our answer, if it satisfies our given condition in the problem then we store its value and reduce the search space accordingly. Prerequisite: Binary Search
Let's understand this via an example:
Given a positive number X, the task is to find out the maximum positive integer that satisfies the given equation: n2 < X
Input: X = 101
Output: 10
Take n = 4: n2 = 16 and 16 < 101 and ans < n , so ans = 4.
then take n = 2: n2 = 4 and 4 < 101 but ans > n so continue.
then take n = 6: n2 = 36 and 36 < 101 and ans < n so ans = 6.
then take n = 15: n2 = 225 and 225 > 101 so ans = 6.
.
.
.
.
and so on...
We can see if we repeatedly do this process we will reach closer to our answer !! But is this an efficient way ?? "No". By using random number we are calculating the same values of n multiple times which will increase out complexity unnecessarily.
In this approach we will only calculate the value of n single time and move forward and if any point we find the equation is true we update our answer.
Initialize ans = 0.
Take n = 1: 12 < 101 , ans = 1
take n = 2: 22 < 101 , ans =2
take n = 3: 32 < 101 , ans =3
.
.
take n = 10: 102 < 101 , ans = 10
take n = 11: 112 > 101 , ans = 10 .
.
.
take n = 101: 1012 > 101 , ans = 10.
This is better approach than previous one because now our time complexity will be O(X) only, because we are checking answer for each value between 0 to X .
Now we will find our answer using the monotonic behaviour of the equation !!
:
- Take n = 50: 502 > 101 , ans = 0 , lower limit = 0 and high limit = 49 ( Because we know if 50 does not satisfy our equation then values greater than 50 will not provide us valid answer ).
- Taking n = 25 (mid value of lower lim. and high lim.) : 252 < 101 , ans = 0 , lower limit = 0 and high limit = 24.
- Taking n = 12: 122 > 101 , ans = 0 , lower limit = 0 and high limit = 11.
- Taking n = 5: 52 < 101 , ans = 5 , lower limit = 6 and high limit = 11 (Because we know if n=5 satisfy the equation then values less than 5 also give the valid answer).
- Taking n = 8: 82 < 101 , ans = 8 , lower limit = 9 and high limit = 11 .
- Taking n = 10: 102 < 101 , ans = 10 , lower limit = 11 and high limit = 11.
- Taking n = 11: 112 > 101 , ans = 10 , lower limit = 11 and high limit = 10.
By following this approach we can find out our answer in log (X) time which is far better than randomly choosing the value or linearly choosing the values.
This approach is called Binary Search on answer!!
Whenever we are able to identify that the answer of the problem lies between in a range L to R and there is a Monotonic Behaviour of answer in range L to R then we can think to apply binary search on answer.
Steps to Solve the problem:
This searching technique of answer is called Binary Search on answer.
Below is the code of above idea:
Value which satisfies the equation : 10
Time Complexity: O(logN)
Auxiliary Complexity: O(1)
In most of the questions where question asks about Find maximum value of something in minimum X operations or Minimum value of something by maximizing some other parameter more often these questions are Binary Search on Answer.
Let's Take a question for better understanding:
Given N boxes and and each box contains different kind of fruits also each box contains arr[i] fruits. In one hour you can select any one type of fruit and eat at most X fruits of that particular type. You Have exactly H hours to finish all the fruits, the task is to find out the minimum value of X such that you can finish all the fruits in maximum H hours.
Note: H is always greater than or equal to N .
Example:
Input: N = 5 , arr[] = [ 2, 4, 2, 4, 5] , H=8
Output: 3
Explanation:
- hour = 1 : eat fruit at index = 0 , so after an hour arr = [ 0 , 4 , 2 , 4 , 5].
- hour = 2: eat fruit at index = 1 , so after an hour arr = [ 0 , 1 , 2 , 4 , 5].
- hour = 3: eat fruit at index = 2 , so after an hour arr = [ 0 , 1 , 0 , 4 , 5].
- hour = 4: eat fruit at index = 1 , so after an hour arr = [ 0 , 0 , 0 , 4 , 5].
- hour = 5: eat fruit at index = 4 , so after an hour arr = [ 0 , 0, 0 , 4 , 2].
- hour = 6: eat fruit at index = 3 , so after an hour arr = [ 0 , 0 , 0, 1, 2 ].
- hour = 7: eat fruit at index = 4 , so after an hour arr = [ 0 , 0 , 0 , 1 , 0].
- hour = 8: eat fruit at index = 3 , so after an hour arr = [ 0 , 0 , 0 , 0, 0].
Input: N = 1, arr[] = [12], H = 2
Output: 6
Approach: To solve the problem follow the below idea:
Idea: We can apply binary Search on answer to solve this problem.
- find out high limit: We know in the worst case H will be equal to N and in this case we have only one hour to eat ith kind of fruit so our answer will [ be in this case ] maximum element of 'arr' .
- find out low limit: In best case it might happen we do not have any fruit then our low limit will be '0' .
If we can eat X amount of fruit in one hour and it takes T hour to finish all the fruits.
- If T< H: then we can even find the lesser value of x which will also hold the condition .
- Else: we have to increase the value of x because current value of x is insufficient.
Now we know the search space and monotonic relation so we just have to iterate over the ' arr ' array and find total time we will take to eat all fruits if current speed of eating the fruits in one hour is X, if time taken is less than H we can shift our high limit to x-1, otherwise shift low = x+1.
By following above idea we can find out our valid minimum possible speed by which we can eat all fruit in H hour.
Steps to solve the problem:
Below is the implementation for the above approach:
Minimum value of X will be : 7
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
For Better Understanding of the above topic we highly recommend you to solve these problems:
Problem | Post Link | Practice Link |
|---|---|---|
1) Time Crunch Challenge | ||
2) Find minimum subarray length to reduce frequency | ||
3) Maximize minimum sweetness in cake cutting | ||
4) Maximize minimum element of an Array using operations | ||
5) Maximize the minimum element and return it | ||
6) Find the maximum value of the K-th smallest usage value in Array | ||
7) Assign stalls to K cows to maximize the minimum distance between them | ||
8) Minimum time to complete at least K tasks when everyone rest after each task | ||
9) Maximum count of integers to be chosen from given two stacks having sum at most K | ||
10 ) Find the row up to which there are at least K stars in the Diamond Pattern |