Open In App

Least root of given quadratic equation for value greater than equal to K

Given the constants of quadratic equation F(x) = Ax2 + Bx + C as A, B, and C and an integer K, the task is to find the smallest value of root x such that F(x) ? K and x > 0. If no such values exist then print “-1”. It is given that F(x) is a monotonically increasing function.

Examples: 



Input: A = 3, B = 4, C = 5, K = 6
Output: 1
Explanation:
For the given values F(x) = 3x2 + 4x + 5 the minimum value of x is 1, F(x) = 12, which is greater than the given value of K.

Input: A = 3, B = 4, C = 5, K = 150
Output: 7
Explanation: 
For the given values F(x) = 3x2 + 4x + 5 the minimum value of x is 7, F(x) = 180, which is greater than the given value of K.



Approach: The idea is to use Binary Search to find the minimum value of x. Below are the steps:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate value of
// quadratic equation for some x
int func(int A, int B, int C, int x)
{
    return (A * x * x + B * x + C);
}
 
// Function to calculate the minimum
// value of x such that F(x) >= K using
// binary search
int findMinx(int A, int B, int C, int K)
{
 
    // Start and end value for
    // binary search
    int start = 1;
    int end = ceil(sqrt(K));
 
    // Binary Search
    while (start <= end) {
        int mid = start + (end - start) / 2;
 
        // Computing F(mid) and F(mid-1)
        int x = func(A, B, C, mid);
        int Y = func(A, B, C, mid - 1);
 
        // Checking the three cases
 
        // If F(mid) >= K  and
        // F(mid-1) < K return mid
        if (x >= K && Y < K) {
            return mid;
        }
 
        // If F(mid) < K go to mid+1 to end
        else if (x < K) {
            start = mid + 1;
        }
 
        // If F(mid) > K go to start to mid-1
        else {
            end = mid - 1;
        }
    }
 
    // If no such value exist
    return -1;
}
 
// Driver Code
int main()
{
    // Given coefficients of Equations
    int A = 3, B = 4, C = 5, K = 6;
 
    // Find minimum value of x
    cout << findMinx(A, B, C, K);
    return 0;
}




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




# Python3 program for the above approach
import math
 
# Function to calculate value of
# quadratic equation for some x
def func(A, B, C, x):
     
    return (A * x * x + B * x + C)
 
# Function to calculate the minimum
# value of x such that F(x) >= K using
# binary search
def findMinx(A, B, C, K):
 
    # Start and end value for
    # binary search
    start = 1
    end = math.ceil(math.sqrt(K))
 
    # Binary Search
    while (start <= end):
        mid = start + (end - start) // 2
 
        # Computing F(mid) and F(mid-1)
        x = func(A, B, C, mid)
        Y = func(A, B, C, mid - 1)
 
        # Checking the three cases
 
        # If F(mid) >= K and
        # F(mid-1) < K return mid
        if (x >= K and Y < K):
            return mid
         
        # If F(mid) < K go to mid+1 to end
        elif (x < K):
            start = mid + 1
         
        # If F(mid) > K go to start to mid-1
        else:
            end = mid - 1
     
    # If no such value exist
    return -1
 
# Driver Code
 
# Given coefficients of Equations
A = 3
B = 4
C = 5
K = 6
 
# Find minimum value of x
print(findMinx(A, B, C, K))
 
# This code is contributed by code_hunt




// C# program for the above approach
using System;
class GFG{
 
// Function to calculate value of
// quadratic equation for some x
static int func(int A, int B, int C, int x)
{
    return (A * x * x + B * x + C);
}
 
// Function to calculate the minimum
// value of x such that F(x) >= K using
// binary search
static int findMinx(int A, int B, int C, int K)
{
     
    // Start and end value for
    // binary search
    int start = 1;
    int end = (int)Math.Ceiling(Math.Sqrt(K));
 
    // Binary Search
    while (start <= end)
    {
        int mid = start + (end - start) / 2;
 
        // Computing F(mid) and F(mid-1)
        int x = func(A, B, C, mid);
        int Y = func(A, B, C, mid - 1);
 
        // Checking the three cases
        // If F(mid) >= K and
        // F(mid-1) < K return mid
        if (x >= K && Y < K)
        {
            return mid;
        }
 
        // If F(mid) < K go to mid+1 to end
        else if (x < K)
        {
            start = mid + 1;
        }
 
        // If F(mid) > K go to start to mid-1
        else
        {
            end = mid - 1;
        }
    }
     
    // If no such value exist
    return -1;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given coefficients of Equations
    int A = 3, B = 4, C = 5, K = 6;
     
    // Find minimum value of x
    Console.WriteLine(findMinx(A, B, C, K));
}
}
 
// This code is contributed by sapnasingh4991




<script>
 
// javascript program for the above approach
 
// Function to calculate value of
// quadratic equation for some x
function func(A , B , C , x)
{
    return (A * x * x + B * x + C);
}
 
// Function to calculate the minimum
// value of x such that F(x) >= K using
// binary search
function findMinx(A , B , C , K)
{
     
    // Start and end value for
    // binary search
    var start = 1;
    var end = parseInt(Math.ceil(Math.sqrt(K)));
 
    // Binary Search
    while (start <= end)
    {
        var mid = start + parseInt((end - start) / 2);
 
        // Computing F(mid) and F(mid-1)
        var x = func(A, B, C, mid);
        var Y = func(A, B, C, mid - 1);
 
        // Checking the three cases
        // If F(mid) >= K and
        // F(mid-1) < K return mid
        if (x >= K && Y < K)
        {
            return mid;
        }
 
        // If F(mid) < K go to mid+1 to end
        else if (x < K)
        {
            start = mid + 1;
        }
 
        // If F(mid) > K go to start to mid-1
        else
        {
            end = mid - 1;
        }
    }
     
    // If no such value exist
    return -1;
}
 
// Driver code
 
// Given coefficients of Equations
var A = 3, B = 4, C = 5, K = 6;
 
// Find minimum value of x
document.write(findMinx(A, B, C, K));
 
// This code is contributed by shikhasingrajput
</script>

 
 

Output: 
1

 

Time Complexity: O(log(sqrt(K))
Auxiliary Space: O(1) 

 


Article Tags :