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. If no such values exist then print “-1”. It is given that F(x) is a monotonically increasing function.
Input: A = 3, B = 4, C = 5, K = 6
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
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:
- 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.
Below is the implementation of the above approach:
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.
- 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
- Boundary Value Analysis : Nature of Roots of a Quadratic equation
- Smallest subarray of size greater than K with sum greater than a given value
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Find the quadratic equation from the given roots
- 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
- 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
- Highest and Smallest power of K less than and greater than equal to N respectively
- Maximum and Minimum value of a quadratic function
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Find if two given Quadratic equations have common roots or not
- Smallest Special Prime which is greater than or equal to a given number
- Queries to count array elements greater than or equal to a given number with updates
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.