The Ubiquitous Binary Search | Set 1

14 Jun, 2022
We all aware of binary search algorithm. Binary search is easiest difficult algorithm to get it right. I present some interesting problems that I collected on binary search. There were some requests on binary search. I request you to honor the code, “I sincerely attempt to solve the problem and ensure there are no corner cases”. After reading each problem minimize the browser and try solving it. Problem Statement: Given a sorted array of N distinct elements. Find a key in the array using least number of comparisons. (Do you think binary search is optimal to search a key in sorted array?) Without much theory, here is typical binary search algorithm. 
C
Java
C#
Javascript
// Returns location of key, or -1 if not foundint BinarySearch(int A[], int l, int r, int key){    int m;      while( l <= r )    {        m = l + (r-l)/2;          if( A[m] == key ) // first comparison            return m;          if( A[m] < key ) // second comparison            l = m + 1;        else            r = m - 1;    }      return -1;}
// Java code to implement the approachimport java.util.*;  class GFG {  // Returns location of key, or -1 if not foundstatic int BinarySearch(int A[], int l, int r, int key){    int m;      while( l < r )    {        m = l + (r-l)/2;          if( A[m] == key ) // first comparison            return m;          if( A[m] < key ) // second comparison            l = m + 1;        else            r = m - 1;    }      return -1;}}  // This code is contributed by sanjoy_62.
// C# program to implement// the above approachusing System;  class GFG{  // Returns location of key, or -1 if not foundstatic int BinarySearch(int[] A, int l, int r, int key){    int m;      while( l < r )    {        m = l + (r-l)/2;          if( A[m] == key ) // first comparison            return m;          if( A[m] < key ) // second comparison            l = m + 1;        else            r = m - 1;    }      return -1;}}  // This code is contributed by code_hunt.
<script>// Javascript code to implement the approach    // Returns location of key, or -1 if not foundfunction BinarySearch(A, l, r, key) {  let m;    while (l < r) {    m = l + (r - l) / 2;      if (A[m] == key) // first comparison      return m;      if (A[m] < key) // second comparison      l = m + 1;    else      r = m - 1;  }    return -1;}  // This code is contributed by gfgking</script>
Theoretically we need log N + 1 comparisons in worst case. If we observe, we are using two comparisons per iteration except during final successful match, if any. In practice, comparison would be costly operation, it won’t be just primitive type comparison. It is more economical to minimize comparisons as that of theoretical limit. See below figure on initialize of indices in the next implementation.  The following implementation uses fewer number of comparisons. 
C
// Invariant: A[l] <= key and A[r] > key// Boundary: |r - l| = 1// Input: A[l .... r-1]int BinarySearch(int A[], int l, int r, int key){    int m;      while( r - l > 1 )    {        m = l + (r-l)/2;          if( A[m] <= key )            l = m;        else            r = m;    }      if( A[l] == key )        return l;    if( A[r] == key )        return r;    else        return -1;}
In the while loop we are depending only on one comparison. The search space converges to place l and r point two different consecutive elements. We need one more comparison to trace search status. You can see sample test case http://ideone.com/76bad0. (C++11 code) Problem Statement: Given an array of N distinct integers, find floor value of input ‘key’. Say, A = {-1, 2, 3, 5, 6, 8, 9, 10} and key = 7, we should return 6 as outcome. We can use the above optimized implementation to find floor value of key. We keep moving the left pointer to right most as long as the invariant holds. Eventually left pointer points an element less than or equal to key (by definition floor value). The following are possible corner cases, —> If all elements in the array are smaller than key, left pointer moves till last element. —> If all elements in the array are greater than key, it is an error condition. —> If all elements in the array equal and <= key, it is worst case input to our implementation. Here is implementation, 
C
// largest value <= key// Invariant: A[l] <= key and A[r] > key// Boundary: |r - l| = 1// Input: A[l .... r-1]// Precondition: A[l] <= key <= A[r]int Floor(int A[], int l, int r, int key){    int m;      while( r - l > 1 )    {        m = l + (r - l)/2;          if( A[m] <= key )            l = m;        else            r = m;    }      return A[l];}  // Initial callint Floor(int A[], int size, int key){    // Add error checking if key < A[0]    if( key < A[0] )        return -1;      // Observe boundaries    return Floor(A, 0, size, key);}
You can see some test cases http://ideone.com/z0Kx4a. Problem Statement: Given a sorted array with possible duplicate elements. Find number of occurrences of input ‘key’ in log N time. The idea here is finding left and right most occurrences of key in the array using binary search. We can modify floor function to trace right most occurrence and left most occurrence. Here is implementation, 
C
// Input: Indices Range [l ... r)// Invariant: A[l] <= key and A[r] > keyint GetRightPosition(int A[], int l, int r, int key){    int m;      while( r - l > 1 )    {        m = l + (r - l)/2;          if( A[m] <= key )            l = m;        else            r = m;    }      return l;}  // Input: Indices Range (l ... r]// Invariant: A[r] >= key and A[l] > keyint GetLeftPosition(int A[], int l, int r, int key){    int m;      while( r - l > 1 )    {        m = l + (r - l)/2;          if( A[m] >= key )            r = m;        else            l = m;    }      return r;}  int CountOccurances(int A[], int size, int key){    // Observe boundary conditions    int left = GetLeftPosition(A, -1, size-1, key);    int right = GetRightPosition(A, 0, size, key);      // What if the element doesn't exists in the array?    // The checks helps to trace that element exists    return (A[left] == key && key == A[right])?           (right - left + 1) : 0;}
Sample code http://ideone.com/zn6R6a. Problem Statement: Given a sorted array of distinct elements, and the array is rotated at an unknown position. Find minimum element in the array. We can see  pictorial representation of sample input array in the below figure.  We converge the search space till l and r points single element. If the middle location falls in the first pulse, the condition A[m] < A[r] doesn’t satisfy, we converge our search space to A[m+1 ... r]. If the middle location falls in the second pulse, the condition A[m] < A[r] satisfied, we converge our search space to A[1 ... m]. At every iteration we check for search space size, if it is 1, we are done. Given below is implementation of algorithm. Can you come up with different implementation? 
C
int BinarySearchIndexOfMinimumRotatedArray(int A[], int l, int r){    // extreme condition, size zero or size two    int m;      // Precondition: A[l] > A[r]    if( A[l] <= A[r] )        return l;      while( l <= r )    {        // Termination condition (l will eventually falls on r, and r always        // point minimum possible value)        if( l == r )            return l;          m = l + (r-l)/2; // 'm' can fall in first pulse,                        // second pulse or exactly in the middle          if( A[m] < A[r] )            // min can't be in the range            // (m < i <= r), we can exclude A[m+1 ... r]            r = m;        else            // min must be in the range (m < i <= r),            // we must search in A[m+1 ... r]            l = m+1;    }      return -1;}  int BinarySearchIndexOfMinimumRotatedArray(int A[], int size){    return BinarySearchIndexOfMinimumRotatedArray(A, 0, size-1);}
See sample test cases http://ideone.com/KbwDrk. Exercises: 1. A function called signum(x, y) is defined as,
signum(x, y) = -1 if x < y
             =  0 if x = y
             =  1 if x > y
Did you come across any instruction set in which a comparison behaves like signum function? Can it make the first implementation of binary search optimal? 2. Implement ceil function replica of floor function. 3. Discuss with your friends “Is binary search optimal (results in the least number of comparisons)? Why not ternary search or interpolation search on a sorted array? When do you prefer ternary or interpolation search over binary search?” 4. Draw a tree representation of binary search (believe me, it helps you a lot to understand much internals of binary search).
Stay tuned, I will cover few more interesting problems using binary search in upcoming articles. I welcome your comments. – – – by Venki. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Nitin Jain 7
Aryan_Sapra_280
AjayN
adityac9254
sanjoy_62
code_hunt
gfgking
Binary Search
Searching
Searching
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
K'th Smallest/Largest Element in Unsorted Array | Set 1
Search an element in a sorted and rotated array
Find the Missing Number
Search, insert and delete in an unsorted array
Program to find largest element in an array
k largest(or smallest) elements in an array
Two Pointers Technique
Given an array of size n and a number k, find all elements that appear more than n/k times
Search, insert and delete in a sorted array