Given three numbers a, b, and c that forms a monotonically increasing function of the form a*x2 + b*x + c and two number A and B, the task is to find the root of the function i.e., find the value of x such that where A ≤ x ≤ B.
Input: a = 2, b = -3, c = -2, A = 0, B = 3
f(x) = 2x^2 – 3x – 2 putting the value of x = 2.000
We get f(2.000) = 2(2.000)^2 – 3(2.000) – 2 = 0
Input: a = 2, b = -3, c = -2, A = -2, B = 1
Output: No solution
Approach: Below is the graphical representation of any function:
From the above graph, we have,
- Whenever f(A)*f(B) ≤ 0, it means that the graph of the function will cut the x-axis somewhere within that range and signifies that somewhere there is a point which makes the value of the function as 0 and then the graph proceeds to be negative y-axis.
- If f(A)*f(B) > 0, it means that in this range [A, B] the y values of f(A) and f(B) both remain positive throughout, so they never cut x-axis.
Therefore, from the above observation, the idea is to use Binary Search to solve this problem. Using the given range of [A, B] as lower and upper bound for the root of the equation, x can be found out by applying binary search on this range. Below are the steps:
- Find the middle(say mid) of the range [A, B].
- If f(mid)*f(A) ≤ 0 is true then search space is reduced to [A, mid], because the cut at x-axis has been occurred in this segment.
- Else search space is reduced to [mid, B]
- The minimum possible value for root is when the high value becomes just smaller than EPS(smallest value ~10-6) + lower bound value i.e., fabs(high – low) > EPS.
- Print the value of the root.
Below is the implementation of the above approach:
Time Complexity: O(log(B – A))
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.
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Find the non decreasing order array from given array
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Make the array non-decreasing with the given operation
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Largest number smaller than or equal to n and digits in non-decreasing order
- Total number of non-decreasing numbers with n digits
- Tidy Number (Digits in non-decreasing Order)
- Count permutation such that sequence is non decreasing
- Make the list non-decreasing by changing only one digit of the elements
- Number of non-decreasing sub-arrays of length K
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Count of non decreasing arrays of length N formed with values in range L to R
- Count non decreasing subarrays of size N from N Natural numbers
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Minimize sum of prime numbers added to make an array non-decreasing
- Find the maximum element in an array which is first increasing and then decreasing
- Count of integers up to N which are non divisors and non coprime with N
- Find the count of Strictly decreasing Subarrays
- Find square root of number upto given precision using binary search
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.