VOOZH about

URL: https://www.geeksforgeeks.org/dsa/binary-search-on-answer-tutorial-with-problems/

⇱ Binary Search on Answer Tutorial with Problems - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Binary Search on Answer Tutorial with Problems

Last Updated : 23 Jul, 2025

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

  • Initialize ans = 0 then take one element randomly from 0 to 101
  • Check if this number satisfies the equation or not and update the answer.

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 .

  • We can observe a monotonic behaviour in this method that before n=10 each value satisfy the equation but after n = 10 each value gives false for the equation.
  • We know the equation is monotonic and we know the search space so we now will think about finding our answer using these two properties !!

:

Now we will find our answer using the monotonic behaviour of the equation !!

  • Search space: Lower limit = 0, High limit = 101
  • Initial answer = 0 .

:

  • 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!!

How to identify 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.

  • We are Given a number X and also given numbers are positive
  • Finding value of L [ lower limit of search space ]:
    • We have to think about the minimum value which can satisfy the above equation!!
    • in the above problem it will be 0 because it is the minimum non negative value which satisfy the equation.
  • Finding value of R [ upper limit of search space]:
    • We have to think about the maximum value which can satisfy the above equation!!
    • in above problem it will be X because any value which is greater than X can never satisfy the equation .
  • Given equation : n2 < X .
  • lets say our maximum valued integer is 'K' which satisfies the given equation , then we know that values < K will return True for the above inequality and values greater than K will give false for the same.
  • so this shows our relation is monotonic in nature.

Steps to Solve the problem:

  • Initialize our low and high values as 0 and X.
  • While low<= high, do the following:
  • Find mid value and check its validity for the equation
  • If mid*mid < X:
    • Store the value of mid as our valid answer
    • And reduce search space by updating low = mid+1
  • Else, reduce search space by moving high = mid - 1
  • At the end return the valid answer.

This searching technique of answer is called Binary Search on answer.

Below is the code of above idea:


Output
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:

  • Apply Binary Search on X [answer].
  • Initialize low value = 0 and high = maximum element in arr.
  • Take X = ( low+high )/2;
  • For every value of X traverse whole array and find out the time required to eat all the fruits
  • If req_time < H: Then store the value of X and reduce our search space by making high = X-1;
  • Else: increase the value of low .

Below is the implementation for the above approach:


Output
Minimum value of X will be : 7

Time Complexity: O(N*logN)
Auxiliary Space: O(1)

  • The constraint on the array size is within a reasonable range, it is often less than or equal to 105.
  • The problem states a monotonic behaviour , where the target value can be achieved based on some specific condition.
  • The problem requires finding the minimum of maximums, maximum of minimums.

For Better Understanding of the above topic we highly recommend you to solve these problems:

Problem

Post Link

Practice Link

1) Time Crunch Challenge

Link


2) Find minimum subarray length to reduce frequency

Link


3) Maximize minimum sweetness in cake cutting

Link


4) Maximize minimum element of an Array using operations

Link


5) Maximize the minimum element and return it

Link


6) Find the maximum value of the K-th smallest usage value in Array

Link

Link

7) Assign stalls to K cows to maximize the minimum distance between them

Link

Link

8) Minimum time to complete at least K tasks when everyone rest after each task

Link


9) Maximum count of integers to be chosen from given two stacks having sum at most K

Link


10 ) Find the row up to which there are at least K stars in the Diamond Pattern

Link


Comment
Article Tags: