Abstraction of Binary Search | Java 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
import java . util . * ; class GFG { static void findX ( int targetValue ) { int start = 0 , end = targetValue ; int mid = 0 , result = 0 ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; if ( mid * mid <= targetValue ) { result = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } System . out . print ( result + "NEW_LINE"); } public static void main ( String [ ] args ) { int targetValue = 81 ; findX ( targetValue ) ; } }