Least root of given quadratic equation for value greater than equal to K | Java program for the above approach ; Function to calculate value of quadratic equation for some x ; Function to calculate the minimum value of x such that F ( x ) >= K using binary search ; Start and end value for binary search ; Binary Search ; Computing F ( mid ) and F ( mid - 1 ) ; If F ( mid ) >= K and F ( mid - 1 ) < K return mid ; If F ( mid ) < K go to mid + 1 to end ; If F ( mid ) > K go to start to mid - 1 ; If no such value exist ; Driver code ; Given coefficients of Equations ; Find minimum value of x
import java . util . * ; class GFG { static int func ( int A , int B , int C , int x ) { return ( A * x * x + B * x + C ) ; } static int findMinx ( int A , int B , int C , int K ) { int start = 1 ; int end = ( int ) Math . ceil ( Math . sqrt ( K ) ) ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; int x = func ( A , B , C , mid ) ; int Y = func ( A , B , C , mid - 1 ) ; if ( x >= K && Y < K ) { return mid ; } else if ( x < K ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } public static void main ( String [ ] args ) { int A = 3 , B = 4 , C = 5 , K = 6 ; System . out . println ( findMinx ( A , B , C , K ) ) ; } }