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
using System ; class GFG { static int n = 8 ; static int [ ] a = { 2 , 3 , 3 , 5 , 5 , 5 , 6 , 6 } ; static int contains ( int low , int high , int key ) { int ans = 0 ; 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 = 1 ; break ; } } return ans ; } static 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 ; } static 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 ; } static 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 ; } static 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 ; } static void Main ( ) { Console . WriteLine ( " Contains " ) ; for ( int i = 0 ; i < 10 ; i ++ ) Console . WriteLine ( i + " ▁ " + contains ( 0 , n - 1 , i ) ) ; Console . WriteLine ( " First ▁ occurrence ▁ of ▁ key " ) ; for ( int i = 0 ; i < 10 ; i ++ ) Console . WriteLine ( i + " ▁ " + first ( 0 , n - 1 , i ) ) ; Console . WriteLine ( " Last ▁ occurrence ▁ of ▁ key " ) ; for ( int i = 0 ; i < 10 ; i ++ ) Console . WriteLine ( i + " ▁ " + last ( 0 , n - 1 , i ) ) ; Console . WriteLine ( " Least ▁ integer ▁ greater ▁ than ▁ key " ) ; for ( int i = 0 ; i < 10 ; i ++ ) Console . WriteLine ( i + " ▁ " + leastgreater ( 0 , n - 1 , i ) ) ; Console . WriteLine ( " Greatest ▁ integer ▁ lesser ▁ than ▁ key " ) ; for ( int i = 0 ; i < 10 ; i ++ ) Console . WriteLine ( i + " ▁ " + greatestlesser ( 0 , n - 1 , i ) ) ; } }