Variants of Binary Search | C ++ program to variants of Binary Search ; array size ; Sorted array ; Find if key is in array * Returns : True if key belongs to array , * False if key doesn 't belong to array  ; if mid is less than key , all elements in range [ low , mid ] are also less so we now search in [ mid + 1 , high ] ; if mid is greater than key , all elements in range [ mid + 1 , high ] are also greater so we now search in [ low , mid - 1 ] ; comparison added just for the sake of clarity if mid is equal to key , we have found that key exists in array ; Find first occurrence index of key in array * Returns : an index in range [ 0 , n - 1 ] if key belongs * to array , - 1 if key doesn 't belong to array   ; if mid is less than key , all elements in range [ low , mid ] are also less so we now search in [ mid + 1 , high ] ; if mid is greater than key , all elements in range [ mid + 1 , high ] are also greater so we now search in [ low , mid - 1 ] ; if mid is equal to key , we note down the last found index then we search for more in left side of mid so we now search in [ low , mid - 1 ] ; Find last occurrence index of key in array * Returns : an index in range [ 0 , n - 1 ] if key belongs to array , * - 1 if key doesn 't belong to array   ; if mid is less than key , then all elements in range [ low , mid - 1 ] are also less so we now search in [ mid + 1 , high ] ; if mid is greater than key , then all elements in range [ mid + 1 , high ] are also greater so we now search in [ low , mid - 1 ] ; if mid is equal to key , we note down the last found index then we search for more in right side of mid so we now search in [ mid + 1 , high ] ; Find index of first occurrence of least element greater than key in array * Returns : an index in range [ 0 , n - 1 ] if key is not the greatest element in array , * - 1 if key is the greatest element in array ; if mid is less than key , all elements in range [ low , mid - 1 ] are <= key then we search in right side of mid so we now search in [ mid + 1 , high ] ; if mid is greater than key , all elements in range [ mid + 1 , high ] are >= key we note down the last found index , then we search in left side of mid so we now search in [ low , mid - 1 ] ; if mid is equal to key , all elements in range [ low , mid ] are <= key so we now search in [ mid + 1 , high ] ; Find index of last occurrence of greatest element less than key in array * Returns : an index in range [ 0 , n - 1 ] if key is not the least element in array , * - 1 if key is the least element in array ; if mid is less than key , all elements in range [ low , mid - 1 ] are < key we note down the last found index , then we search in right side of mid so we now search in [ mid + 1 , high ] ; if mid is greater than key , all elements in range [ mid + 1 , high ] are > key then we search in left side of mid so we now search in [ low , mid - 1 ] ; if mid is equal to key , all elements in range [ mid + 1 , high ] are >= key then we search in left side of mid so we now search in [ low , mid - 1 ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n = 8 ; int a [ ] = { 2 , 3 , 3 , 5 , 5 , 5 , 6 , 6 } ; bool contains ( int low , int high , int key ) { bool ans = false ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int midVal = a [ mid ] ; if ( midVal < key ) { low = mid + 1 ; } else if ( midVal > key ) { high = mid - 1 ; } else if ( midVal == key ) { ans = true ; break ; } } return ans ; } int first ( int low , int high , int key ) { int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low + 1 ) / 2 ; int midVal = a [ mid ] ; if ( midVal < key ) { low = mid + 1 ; } else if ( midVal > key ) { high = mid - 1 ; } else if ( midVal == key ) { ans = mid ; high = mid - 1 ; } } return ans ; } int last ( int low , int high , int key ) { int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low + 1 ) / 2 ; int midVal = a [ mid ] ; if ( midVal < key ) { low = mid + 1 ; } else if ( midVal > key ) { high = mid - 1 ; } else if ( midVal == key ) { ans = mid ; low = mid + 1 ; } } return ans ; } int leastgreater ( int low , int high , int key ) { int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low + 1 ) / 2 ; int midVal = a [ mid ] ; if ( midVal < key ) { low = mid + 1 ; } else if ( midVal > key ) { ans = mid ; high = mid - 1 ; } else if ( midVal == key ) { low = mid + 1 ; } } return ans ; } int greatestlesser ( int low , int high , int key ) { int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low + 1 ) / 2 ; int midVal = a [ mid ] ; if ( midVal < key ) { ans = mid ; low = mid + 1 ; } else if ( midVal > key ) { high = mid - 1 ; } else if ( midVal == key ) { high = mid - 1 ; } } return ans ; } int main ( ) { printf ( " Contains STRNEWLINE " ) ; for ( int i = 0 ; i < 10 ; i ++ ) printf ( " % d ▁ % d STRNEWLINE " , i , contains ( 0 , n - 1 , i ) ) ; printf ( " First ▁ occurrence ▁ of ▁ key STRNEWLINE " ) ; for ( int i = 0 ; i < 10 ; i ++ ) printf ( " % d ▁ % d STRNEWLINE " , i , first ( 0 , n - 1 , i ) ) ; printf ( " Last ▁ occurrence ▁ of ▁ key STRNEWLINE " ) ; for ( int i = 0 ; i < 10 ; i ++ ) printf ( " % d ▁ % d STRNEWLINE " , i , last ( 0 , n - 1 , i ) ) ; printf ( " Least ▁ integer ▁ greater ▁ than ▁ key STRNEWLINE " ) ; for ( int i = 0 ; i < 10 ; i ++ ) printf ( " % d ▁ % d STRNEWLINE " , i , leastgreater ( 0 , n - 1 , i ) ) ; printf ( " Greatest ▁ integer ▁ lesser ▁ than ▁ key STRNEWLINE " ) ; for ( int i = 0 ; i < 10 ; i ++ ) printf ( " % d ▁ % d STRNEWLINE " , i , greatestlesser ( 0 , n - 1 , i ) ) ; return 0 ; }