Given a quartic equation in the form , determine the absolute difference between the sum of its roots and the product of its roots. Note that roots need not be real – they can also be complex.

**Examples:**

Input: 4x^4 + 3x^3 + 2x^2 + x - 1 Output: 0.5 Input: x^4 + 4x^3 + 6x^2 + 4x + 1 Output: 5

**Approach:** Solving the quartic equation to obtain each individual root would be time-consuming and inefficient, and would require much effort and computational power. A more efficient solution utilises the following formulae:

The quartic always has sum of roots , and product of roots .

Hence by computing we find the absolute difference between sum and product of roots.

**Below is the implementation of above approach:**

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function taking coefficient of ` `// each term of equation as input ` `double` `sumProductDifference(` `int` `a, ` `int` `b, ` ` ` `int` `c, ` `int` `d, ` ` ` `int` `e) ` `{ ` ` ` `// Finding sum of roots ` ` ` `double` `rootSum = (` `double` `)(-1 * b) / a; ` ` ` ` ` `// Finding product of roots ` ` ` `double` `rootProduct = (` `double` `) e / a; ` ` ` ` ` `// Absolute difference ` ` ` `return` `abs` `(rootSum - rootProduct); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `cout << sumProductDifference(8, 4, 6, 4, 1); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed ` `// by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `public` `class` `GFG { ` ` ` `// Function taking coefficient of ` ` ` `// each term of equation as input ` ` ` `static` `double` `sumProductDifference(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d, ` `int` `e) { ` ` ` `// Finding sum of roots ` ` ` `double` `rootSum = (` `double` `)(-` `1` `* b) / a; ` ` ` ` ` `// Finding product of roots ` ` ` `double` `rootProduct = (` `double` `) e / a; ` ` ` ` ` `// Absolute difference ` ` ` `return` `Math.abs(rootSum - rootProduct); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) { ` ` ` `System.out.println(sumProductDifference(` `8` `, ` `4` `, ` `6` `, ` `4` `, ` `1` `)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of above approach ` ` ` `# Function taking coefficient of ` `# each term of equation as input ` `def` `sumProductDifference(a, b, c, d, e): ` ` ` ` ` `# Finding sum of roots ` ` ` `rootSum ` `=` `(` `-` `1` `*` `b)` `/` `a ` ` ` ` ` `# Finding product of roots ` ` ` `rootProduct ` `=` `e ` `/` `a ` ` ` ` ` `# Absolute difference ` ` ` `return` `abs` `(rootSum` `-` `rootProduct) ` ` ` `print` `(sumProductDifference(` `8` `, ` `4` `, ` `6` `, ` `4` `, ` `1` `)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function taking coefficient of ` `// each term of equation as input ` `static` `double` `sumProductDifference(` `int` `a, ` `int` `b, ` ` ` `int` `c, ` `int` `d, ` ` ` `int` `e) ` `{ ` ` ` `// Finding sum of roots ` ` ` `double` `rootSum = (` `double` `)(-1 * b) / a; ` ` ` ` ` `// Finding product of roots ` ` ` `double` `rootProduct = (` `double` `) e / a; ` ` ` ` ` `// Absolute difference ` ` ` `return` `Math.Abs(rootSum - rootProduct); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `Console.Write(sumProductDifference(8, 4, 6, 4, 1)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function taking coefficient of ` `// each term of equation as input ` `function` `sumProductDifference(` `$a` `, ` `$b` `, ` ` ` `$c` `, ` `$d` `,` `$e` `) ` `{ ` ` ` `// Finding sum of roots ` ` ` `$rootSum` `= (double)(-1 * ` `$b` `) / ` `$a` `; ` ` ` ` ` `// Finding product of roots ` ` ` `$rootProduct` `= (double) ` `$e` `/ ` `$a` `; ` ` ` ` ` `// Absolute difference ` ` ` `return` `abs` `(` `$rootSum` `- ` `$rootProduct` `); ` `} ` ` ` `// Driver code ` `echo` `sumProductDifference(8, 4, 6, 4, 1); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.625

**Explanation:** The input equation is .

By finding , we get ,

which is , or .

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Form the Cubic equation from the given roots
- Find the quadratic equation from the given roots
- Program to find the Roots of Quadratic equation
- Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula
- Check if roots of a Quadratic Equation are reciprocal of each other or not
- Find the integral roots of a given Cubic equation
- Boundary Value Analysis : Nature of Roots of a Quadratic equation
- Pair with min absolute difference and whose product is N+1 or N+2
- Maximize product of absolute index difference with K
- Product of absolute difference of every pair in given Array
- Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the first X and last X Digits of N
- Minimum absolute difference between N and a power of 2
- Minimize the absolute difference of sum of two subsets
- Absolute difference between set and unset bit count in N
- Minimum absolute difference between N and any power of 2
- Sum of absolute difference of all pairs raised to power K
- Absolute Difference of even and odd indexed elements in an Array
- Count pairs in an array such that the absolute difference between them is ≥ K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.