VOOZH about

URL: https://www.geeksforgeeks.org/dsa/binary-search-identify-solve-and-interview-questions/

⇱ Binary Search Technique - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Binary Search Technique

Last Updated : 23 Jul, 2025

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.

What is the Binary Search Technique?

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.

Types of Problems Suitable for Binary Search

Here are four types of problems where Binary Search can or cannot be applied:

1)

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.

2)

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.

3)

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 then we can think to apply binary search on the answer.

4)

In this case we can not apply binary search.

πŸ‘ Identify-and-solve-Binary-search
Identify and Solve Binary Search Problems

Now here is the general technique to Identify and Solve the Binary search problems.

How to Identify 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:

πŸ‘ Monotonic-Function
Monotonic Functions


Now if this problem shows a monotonic behavior, generally this tells you that you can apply a binary search here.

How to Solve Binary Search Problems?

  • Define a search space by pointers let's say low to high.
  • Calculate mid value of search space,and check mid value is true or false.
  • Considering above resut figure out where expected answer should lie, In the left half or right half Then update the search space accordingly.
  • Repeat the above steps till there is search space left to search.

Example on Problems Using Binary search:

Problem Statement : Given an array arr[] of integers and a number x, the task is to find the minimum length of subarray with a sum greater than the given value k.

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.

  • Define search space
    • low = 0,as minimum size of subarray can be zero.
    • high = length of the array, as subarray size can be up to length of array.
  • Calculate mid value as (low+high)/2 and check f(mid) returns true or false.
    • Check sum of every subarray of size mid is greater than x or not using sliding window.
      • if there is a subarray present then f(mid) is true.
      • else f(mid) is false.
  • Update the search space:
    • if f(mid) is true and minimal length to be find then expected answer should lie in left half (high = mid-1) as in the right half of mid all the value are true, store the mid value as best possible answer till now.
    • if f(mid) is false i.e. there is no subarray of size mid,so size need to be increased to increase the sum.Means expected answer should lie in the right half(low =mid+1).
  • Repeat the above steps with updated search space till there is search space to search.

Implementation of the above solution:


Output
3

Time Complexity:O(NlogN),N is the size of the subarray
Auxiliary space: O(1)

Most Common Interview Problems on Binary Search

Problem Name

Practice Link to the Problem

Binary Search

View

Floor in a Sorted Array

View

First and last occurrences of X

View

Peak element

View

Square root of a number

View

Koko Eating Bananas

View

Minimum days to make M bouquets

View

Smallest Divisor

View

Capacity To Ship Packages Within D Days

View

Aggressive Cows

View

Allocate minimum number of pages

View

Median of 2 Sorted Arrays of Different Sizes

View

Comment