# Roots Of A Quadratic Equation

Posted: 20 Nov, 2020

Difficulty: Easy

#### 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.
```

##### Input format:

```
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.
```

##### Constraints:

```
1 <= T <= 10^5
-10^3 <= A, C <= 10^3
-10^4 <= B <= 10^4
A ≠ 0
Time Limit: 1 sec
```

Approach 1

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:

- If ‘D’ is less than 0, we will have imaginary roots, so we will return a pair that contains -1.
- If ‘D’ is equal to 0 we will have repeated roots, so we will return a pair that contains the same value twice.
- If ‘D’ is greater than 0 we will have 2 unique roots. In that case, return the two unique roots.

SIMILAR PROBLEMS

# Count of paths

Posted: 7 Jul, 2021

Difficulty: Hard

# Similar Strings

Posted: 7 Jul, 2021

Difficulty: Ninja

# Palindromes And Indexes

Posted: 7 Jul, 2021

Difficulty: Moderate

# Ceiling in a sorted array

Posted: 8 Jul, 2021

Difficulty: Moderate

# Game of 3

Posted: 11 Jul, 2021

Difficulty: Easy