A **recurrence relation** is an equation that recursively defines a sequence or multidimensional array of values, once one or more initial terms are given; each further term of the sequence or array is defined as a function of the preceding terms. Below are the steps required to solve a recurrence equation using the polynomial reduction method:

- Form a characteristic equation for the given recurrence equation.
- Solve the characteristic equation and find the roots of the characteristic equation.
- Simplify the solution with unknown coefficients.
- Solve the equation with respect to the initial conditions to get a specific solution.

**Example:**

Consider the following second-order recurrence equation:

T(n) = a_{1}T(n-1) + a_{2}T(n-2)

For solving this equation formulate it into a characteristic equation.

Let us rearrange the equation as follows:

T(n) - a_{1}T(n-1) - a_{2}T(n-2) = 0

Let, *T(n) = x ^{n}*

Now we can say that

*T(n-1) = x*and

^{n-1}*T(n-2)=x*

^{n-2}Now the equation will be:

x^{n }+ a_{1}x^{n-1 }+ a_{2}x^{n-2 }= 0

After dividing the whole equation by *x ^{n-2}* [since

*x*is not equal to

*0*] we get:

x^{2 }+ a_{1}x + a_{2 }= 0

We have got our characteristic equation as *x ^{2}+a_{1}x+a_{2}=0 *

Now, find the roots of this equation.

Three cases may exist while finding the roots of the characteristic equation and those are:

**Case 1: Roots of the Characteristic Equation are Real and Distinct**

If there are *r* number of distinct roots for the characteristic equation then it can be said that *r* number of fundamental solutions are possible. One can take any linear combination of roots to get the general solution of a linear recurrence equation.

If* r _{1}, r_{2}, r_{3}……, r_{k}* are the roots of the characteristic equation then the general solution of the recurrence equation will be:

t_{n }= c_{1}r_{1}^{n }+ c_{2}r_{2}^{n }+ c_{3}r_{3}^{n }+...........+ c_{k}r_{k}^{n}

**Case 2: Roots of the Characteristic Equation are Real but not Distinct**

Let us consider the roots of the characteristic equations are not distinct and the roots r is in the multiplicity of m. In this case, the solution of the characteristic equation will be:

t_{n}= r^{n}t_{n}= nr^{n}t_{n}= n^{2}r^{n}.........tn = n^{ m - 1}r^{n}

Therefore, include all the solutions to get a general solution of the given recurrence equation.

**Case 3: Roots of the Characteristic Equation are Distinct but Not Real**

If the roots of the characteristic equation are complex, then find the conjugate pair of roots.

If *r _{1}* and

*r*are the two roots of a characteristic equation, and they are in conjugate pair with each other it can be expressed as:

_{2}r_{1 }= re^{ix}r_{2 }= re^{-ix}

The general solution will be:

t_{n}= c_{1}r^{n}cos nx + c_{2}r^{n}sin nx

**Example:**

Let’s solve the given recurrence relation:

T(n) = 7*T(n-1) - 12*T(n-2)

Let T(n) = x^{n}

Now we can say that T(n-1) = x^{n-1} and T(n-2)=x^{n-2}

And dividing the whole equation by x^{n-2}, we get:

x^{2 }- 7*x + 12= 0

Below is the implementation to solve the given quadratic equation:

## C++

`// C++ program to find roots` `// of a quadratic equation` `#include<bits/stdc++.h>` `using` `namespace` `std;` `class` `Quadratic{` ` ` `public` `:` `// Prints roots of quadratic` `// equation ax * 2 + bx + x` `void` `findRoots(` `int` `a, ` `int` `b, ` `int` `c)` `{` ` ` ` ` `// If a is 0, then equation is not` ` ` `// quadratic, but linear` ` ` `if` `(a == 0)` ` ` `{` ` ` `cout << ` `"Invalid"` `;` ` ` `return` `;` ` ` `}` ` ` `int` `d = b * b - 4 * a * c;` ` ` `float` `sqrt_val = ` `sqrt` `(` `abs` `(d));` ` ` `// Real Roots` ` ` `if` `(d > 0)` ` ` `{` ` ` `cout << ` `"Roots are real and different"` `<< endl;` ` ` `cout << fixed << std::setprecision(1)` ` ` `<< ` `float` `((-b + sqrt_val) / (2 * a)) << endl;` ` ` `cout << fixed << std::setprecision(1)` ` ` `<< ` `float` `((-b - sqrt_val) / (2 * a)) << endl;` ` ` `}` ` ` `// Imaginary Roots` ` ` `else` ` ` `{` ` ` `cout << ` `"Roots are complex"` `<< endl;` ` ` `cout << fixed << std::setprecision(1)` ` ` `<< ` `float` `(b / (2.0 * a)) << ` `" + i"` ` ` `<< sqrt_val << endl;` ` ` `cout << fixed << std::setprecision(1)` ` ` `<< ` `float` `(b / (2.0 * a)) << ` `" - i"` ` ` `<< sqrt_val << endl;` ` ` `}` `}` `};` `// Driver code` `int` `main()` `{` ` ` `Quadratic obj;` ` ` `// Given value of coefficients` ` ` `int` `a = 1, b = -7, c = 12;` ` ` ` ` `obj.findRoots(a, b, c);` `}` `// This code is contributed by SURENDRA_GANGWAR` |

## Java

`// Java program to find roots` `// of a quadratic equation` `import` `java.io.*;` `import` `static` `java.lang.Math.*;` `class` `Quadratic {` ` ` `// Prints roots of quadratic` ` ` `// equation ax * 2 + bx + x` ` ` `void` `findRoots(` `int` `a, ` `int` `b, ` `int` `c)` ` ` `{` ` ` `// If a is 0, then equation is not` ` ` `// quadratic, but linear` ` ` `if` `(a == ` `0` `) {` ` ` `System.out.println(` `"Invalid"` `);` ` ` `return` `;` ` ` `}` ` ` `int` `d = b * b - ` `4` `* a * c;` ` ` `double` `sqrt_val = sqrt(abs(d));` ` ` `// Real Roots` ` ` `if` `(d > ` `0` `) {` ` ` `System.out.println(` ` ` `"Roots are real"` ` ` `+ ` `" and different \n"` `);` ` ` `System.out.println(` ` ` `(` `double` `)(-b + sqrt_val)` ` ` `/ (` `2` `* a)` ` ` `+ ` `"\n"` ` ` `+ (` `double` `)(-b - sqrt_val)` ` ` `/ (` `2` `* a));` ` ` `}` ` ` `// Imaginary Roots` ` ` `else` `{` ` ` `System.out.println(` ` ` `"Roots are complex \n"` `);` ` ` `System.out.println(` ` ` `-(` `double` `)b / (` `2` `* a)` ` ` `+ ` `" + i"` ` ` `+ sqrt_val + ` `"\n"` ` ` `+ -(` `double` `)b / (` `2` `* a)` ` ` `+ ` `" - i"` `+ sqrt_val);` ` ` `}` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `Quadratic obj = ` `new` `Quadratic();` ` ` `// Given value of coefficients` ` ` `int` `a = ` `1` `, b = -` `7` `, c = ` `12` `;` ` ` `obj.findRoots(a, b, c);` ` ` `}` `}` |

## Python3

`# Python3 program to find roots` `# of a quadratic equation` `from` `math ` `import` `sqrt` `# Prints roots of quadratic` `# equation ax * 2 + bx + x` `def` `findRoots(a, b, c):` ` ` ` ` `# If a is 0, then equation is not` ` ` `# quadratic, but linear` ` ` `if` `(a ` `=` `=` `0` `):` ` ` `print` `(` `"Invalid"` `)` ` ` `return` ` ` `d ` `=` `b ` `*` `b ` `-` `4` `*` `a ` `*` `c` ` ` `sqrt_val ` `=` `sqrt(` `abs` `(d))` ` ` `# Real Roots` ` ` `if` `(d > ` `0` `):` ` ` `print` `(` `"Roots are real and different"` `)` ` ` `print` `((` `-` `b ` `+` `sqrt_val) ` `/` `(` `2` `*` `a))` ` ` `print` `((` `-` `b ` `-` `sqrt_val) ` `/` `(` `2` `*` `a))` ` ` `# Imaginary Roots` ` ` `else` `:` ` ` `print` `(` `"Roots are complex \n"` `)` ` ` `print` `(` `-` `b ` `/` `(` `2` `*` `a), ` `" + i"` `,` ` ` `sqrt_val, ` `"\n"` `,` ` ` `-` `b ` `/` `(` `2` `*` `a), ` `" - i"` `,` ` ` `sqrt_val)` ` ` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given value of coefficients` ` ` `a ` `=` `1` ` ` `b ` `=` `-` `7` ` ` `c ` `=` `12` ` ` ` ` `findRoots(a, b, c)` `# This code is contributed by mohit kumar 29` |

## C#

`// C# program to find roots` `// of a quadratic equation` `using` `System;` `class` `Quadratic{` `// Prints roots of quadratic` `// equation ax * 2 + bx + x` `void` `findRoots(` `int` `a,` ` ` `int` `b, ` `int` `c)` `{` ` ` `// If a is 0, then equation` ` ` `// is not quadratic, but linear` ` ` `if` `(a == 0)` ` ` `{` ` ` `Console.WriteLine(` `"Invalid"` `);` ` ` `return` `;` ` ` `}` ` ` `int` `d = b * b - 4 *` ` ` `a * c;` ` ` `double` `sqrt_val =` ` ` `Math.Sqrt(Math.Abs(d));` ` ` `// Real Roots` ` ` `if` `(d > 0)` ` ` `{` ` ` `Console.WriteLine(` `"Roots are real"` `+` ` ` `" and different \n"` `);` ` ` `Console.WriteLine((` `double` `)` ` ` `(-b + sqrt_val) /` ` ` `(2 * a) + ` `"\n"` `+` ` ` `(` `double` `)` ` ` `(-b - sqrt_val) /` ` ` `(2 * a));` ` ` `}` ` ` `// Imaginary Roots` ` ` `else` ` ` `{` ` ` `Console.WriteLine(` `"Roots are complex \n"` `);` ` ` `Console.WriteLine(-(` `double` `)b / (2 * a) +` ` ` `" + i"` `+ sqrt_val +` ` ` `"\n"` `+ -(` `double` `)b /` ` ` `(2 * a) + ` `" - i"` `+` ` ` `sqrt_val);` ` ` `}` `}` `// Driver code` `public` `static` `void` `Main(String []args)` `{` ` ` `Quadratic obj = ` `new` `Quadratic();` ` ` `// Given value of coefficients` ` ` `int` `a = 1, b = -7, c = 12;` ` ` `obj.findRoots(a, b, c);` `}` `}` `// This code is contributed by Princi Singh` |

**Output**

Roots are real and different 4.0 3.0

**Time Complexity:** O(√N) **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****.**