Abstraction of Binary Search | C ++ program for the above example ; Function to find X such that it is less than the target value and function is f ( x ) = x ^ 2 ; Initialise start and end ; Loop till start <= end ; Find the mid ; Check for the left half ; Store the result ; Reinitialize the start point ; Check for the right half ; Print the maximum value of x such that x ^ 2 is less than the targetValue ; Driver Code ; Given targetValue ; ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void findX ( int targetValue ) { int start = 0 , end = targetValue ; int mid , result ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; if ( mid * mid <= targetValue ) { result = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } cout << result << endl ; } int main ( ) { int targetValue = 81 ; findX ( targetValue ) ; }