Given two integers S and M, the task is to find the coefficients of the quadratic equation such that the sum and the product of the roots are S and M respectively.
Input: S = 5, M = 6
Output: 1 -5 6
For the quadratic equation x2 – 5x + 6 = 0. The root of the equation are 2 and 3. Therefore, the sum of roots is 2 + 3 = 5, and the product of roots is 2*3 = 6.
Input: S = -2, M = 1
Output: 1 2 1
Approach: The given problem can be solved by using the property of the Quadratic Equation as shown below:
For the above quadratic equation the roots are given by:
The sum of roots is given by:
The product of roots is given by:
From the above two equations, if the value of a is 1 then the value of b is (-1)*S, and c is P. Therefore, the equation is given by:
Below is the implementation of the above approach:
1 -5 6
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.