1

Roots of a Quadratic Equation

Difficulty: EASY
Avg. time to solve
15 min
Success Rate
80%

Problem Statement
Suggest Edit

Given 3 integers a,b,c which are the coefficients of the quadratic equation (ax^2+bx+c=0). 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.

We can consider the equation x^-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. Do not print anything, just return the roots of the quadratic equation or -1 if no roots exist.

2. If the equation has repeated roots, return the roots twice.

3. If there are imaginary roots, return -1 twice.

4. If the root is not an integer, return the greatest integer value less than the root.

5. a,b,c are integers
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

Each test case consists of three space-separated integers a,b,c which denotes the coefficients int the polynomial: ax^2+bx+c=0.
Output Format:
For each test case, return a pair of integers, denoting the roots of the quadratic equation. If the roots are imaginary, return a pair of -1. If there are repeated roots, return a pair with the same integer twice.
Constraints:
1 <= T <= 10^5
-10^3 <= a,c <= 10^3
-10^4 <= b <= 10^4
 a≠0


Where ‘T’ is the total number of test cases, ‘a’,’b’,’c’ denotes the coefficients in the quadratic equation: ax^2+bx+c

Time limit: 1 second
Sample Input 1:
3
1 -5 6
1 -2 -2
1 0 1 
Sample Output 1:
2 3
-1 2
-1 -1
Explanation of sample input 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.
Sample Input 2:
2
3 -6 -18
1 -7 12
Sample Output 2:
-2 3
3 4
Want to solve this problem? Login now to get access to solve the problems