Given the constants of quadratic equation **F(x) = Ax ^{2} + 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**. 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 = 6Output:1Explanation:

For the given values F(x) = 3x^{2}+ 4x + 5 the minimum value of x is 1, F(x) = 12, which is greater than the given value ofK.

Input:A = 3, B = 4, C = 5, K = 150Output:7Explanation:

For the given values F(x) = 3x^{2}+ 4x + 5 the minimum value of x is 7, F(x) = 180, which is greater than the given value ofK.

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

- To get the value equal to or greater than
**K,**the value of**x**must be in the range**[1, sqrt(K)]**as this is a quadratic equation. - Now, basically there is a need to search the appropriate element in the range, so for this binary search is implemented.
- Computing
**F(mid)**, where**mid**is the middle value for the range**[1, sqrt(K)]**. Now the following three cases are possible:- If
**F(mid) ≥ K && F(mid) < K:**This mean the current mid is the required answer. - If
**F(mid) < K:**This means the current value of mid is less than the required value of x. So, move towards the right, i.e., in the second half as**F(x)**is an increasing function.

- If
**F(mid) > K:**This means the current value of mid is greater than the required value of x. So, move towards the left, i.e., the first half as**F(x)**is an increasing function.

- If

Below is the implementation of the above approach:

## C++

`// 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; ` `}` |

*chevron_right*

*filter_none*

## Java

`// 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 ` |

*chevron_right*

*filter_none*

**Output:**

1

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check whether one root of the Quadratic Equation is twice of other or not
- Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not
- Find the quadratic equation from the given roots
- Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula
- Check if roots of a Quadratic Equation are reciprocal of each other or not
- Program to find the Roots of Quadratic equation
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Program to find number of solutions in Quadratic Equation
- Boundary Value Analysis : Nature of Roots of a Quadratic equation
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Smallest power of 4 greater than or equal to N
- Longest Subarray with Sum greater than Equal to Zero
- Smallest number greater than or equal to N divisible by K
- Longest subarray having average greater than or equal to x
- Highest and Smallest power of K less than and greater than equal to N respectively
- Smallest number greater than or equal to N using only digits 1 to K
- Smallest number greater than Y with sum of digits equal to X
- Smallest subarray from a given Array with sum greater than or equal to K
- Replace two consecutive equal values with one greater
- Number of non-decreasing sub-arrays of length greater than or equal to K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.