![]() |
VOOZH | about |
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.
Examples:
Input: a = 2, b = -3, c = -2, A = 0, B = 3
Output: 2.0000
Explanation:
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:
👁 Image
From the above graph, we have,
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:
Below is the implementation of the above approach:
2.0000
Time Complexity: O(log(B - A))
Auxiliary Space: O(1)