You have been given 3 integers 'A', 'B', 'C' which are the coefficients of the quadratic equation (AX^2 + BX + C = 0). Your task is to find the real roots of the quadratic equation or report if no real roots exist (return a pair of -1).
For example:
Let’s consider the equation X^2 + 2X + 1 = 0 . We can see that the quadratic equation has no real roots. So we return a pair of -1 i.e. [-1, -1].
We can consider the equation X^2 - 5X - 6 = 0. As depicted from the equation the value of 'A' would be 1, 'B' would be -5 and 'C' would be -6. We can see that this equation has two distinct roots -2 and -3. Hence we return an array/sequence containing -2 and-3.
Note:1. If the equation has repeated roots, return the roots twice.
2. If there are imaginary roots, return -1 twice.
3. If the root is not an integer, return the greatest integer value less
than the root.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first and the only line of each test case consists of three single space-separated integers 'A', 'B', and 'C' which denotes the integer coefficients in the polynomial: AX^2 + BX + C = 0.
Output Format:
For each test case, print in a single line two single space-separated integers, denoting the roots of the quadratic equation.
The output of each test case will be printed in a separate line.
Note
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
1 <= T <= 10^5
-10^3 <= A, C <= 10^3
-10^4 <= B <= 10^4
A ≠ 0
Time Limit: 1 sec
3
1 -5 6
1 -2 -2
1 0 1
2 3
-1 2
-1 -1
As depicted by the first line, there are total of three test cases.
For the first test case, we can see that the quadratic equation is X^2-5X+6=0. Solving the equation we get (X-2)(X-3)=0 which gives us the factors of the given equation, hence the roots are X=2 and X=3. Therefore we return 2 and 3.
For the second test case, we can see that the quadratic equation is X^2-2X-2=0. Solving we get (X-1+√3)(X+1-√3)=0 which gives us the factors of the given equation, hence the roots are X= (1-√3)≈ -0.732051 and X=(1+√3)≈2.732051. Since the roots are not integers, we return the greatest integer less than the roots i.e 0 and 2 respectively.
For the third test case, we can see that the quadratic equation is X^2+1=0. Trying to solve it we get X^2=-1. We know that it is impossible to have the square of a real number equal to a negative number, so this quadratic equation has imaginary roots. Hence we return -1 twice.
2
3 -6 -18
1 -7 12
-2 3
3 4
Try to use the quadratic formula.
Since we need to find the roots of the quadratic equation, we can use the well known quadratic formula :
ROOT1 = (-B + sqrt(D)) / 2 * A
ROOT2 = (-B - sqrt(D)) / 2 * A
Where ‘D’ is the discriminant equal to (B ^ 2) - (4 * A * C).
Now depending on the value of ‘D’, we can have the following cases:
O(1).
We are using constant time per test case, as the constant amount of operations are performed per test case.
O(1).
We are using constant space.