Given a sequence with some of its term, we need to calculate next K term of this sequence. It is given that sequence is generated by some polynomial, however complex that polynomial can be. Notice polynomial is an expression of the following form:

P(x) = a0 + a1 x +a2 x^2 + a3 x^3 …….. + an x^n

The given sequence can always be described by a number of polynomials, among these polynomial we need to find polynomial with lowest degree and generate next terms using this polynomial only.

Examples:

If given sequence is 1, 2, 3, 4, 5 then its next term will be 6, 7, 8, etc and this correspond to a trivial polynomial. If given sequence is 1, 4, 7, 10 then its next term will be 13, 16, etc.

We can solve this problem using a technique called difference of differences method, which is derivable from lagrange polynomial.

The technique is simple, we take the difference between the consecutive terms, if difference are equal then we stop and build up next term of the sequence otherwise we again take the difference between these differences until they become constant.

The technique is explained in below diagram with an example, given sequence is 8, 11, 16, 23 and we are suppose to find next 3 terms of this sequence.

In below code same technique is implemented, first we loop until we get a constant difference keeping first number of each difference sequence in a separate vector for rebuilding the sequence again. Then we add K instance of same constant difference to our array for generating new K term of sequence and we follow same procedure in reverse order to rebuild the sequence.

See below code for better understanding.

## C++

`// C++ code to generate next terms of a given polynomial ` `// sequence ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// method to print next terms term of sequence ` `void` `nextTermsInSequence(` `int` `sequence[], ` `int` `N, ` `int` `terms) ` `{ ` ` ` `int` `diff[N + terms]; ` ` ` ` ` `// first copy the sequence itself into diff array ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `diff[i] = sequence[i]; ` ` ` ` ` `bool` `more = ` `false` `; ` ` ` `vector<` `int` `> first; ` ` ` `int` `len = N; ` ` ` ` ` `// loop untill one difference remains or all ` ` ` `// difference become constant ` ` ` `while` `(len > 1) ` ` ` `{ ` ` ` `// keeping the first term of sequence for ` ` ` `// later rebuilding ` ` ` `first.push_back(diff[0]); ` ` ` `len--; ` ` ` ` ` `// converting the difference to difference ` ` ` `// of differences ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `diff[i] = diff[i + 1] - diff[i]; ` ` ` ` ` `// checking if all difference values are ` ` ` `// same or not ` ` ` `int` `i; ` ` ` `for` `(i = 1; i < len; i++) ` ` ` `if` `(diff[i] != diff[i - 1]) ` ` ` `break` `; ` ` ` ` ` `// If some difference values were not same ` ` ` `if` `(i != len) ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `int` `iteration = N - len; ` ` ` ` ` `// padding terms instance of constant difference ` ` ` `// at the end of array ` ` ` `for` `(` `int` `i = len; i < len + terms; i++) ` ` ` `diff[i] = diff[i - 1]; ` ` ` `len += terms; ` ` ` ` ` `// iterating to get actual sequence back ` ` ` `for` `(` `int` `i = 0; i < iteration; i++) ` ` ` `{ ` ` ` `len++; ` ` ` ` ` `// shifting all difference by one place ` ` ` `for` `(` `int` `j = len - 1; j > 0; j--) ` ` ` `diff[j] = diff[j - 1]; ` ` ` ` ` `// copying actual first element ` ` ` `diff[0] = first[first.size() - i - 1]; ` ` ` ` ` `// converting difference of differences to ` ` ` `// difference array ` ` ` `for` `(` `int` `j = 1; j < len; j++) ` ` ` `diff[j] = diff[j - 1] + diff[j]; ` ` ` `} ` ` ` ` ` `// printing the result ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `cout << diff[i] << ` `" "` `; ` ` ` `cout << endl; ` `} ` ` ` `// Driver code to test above method ` `int` `main() ` `{ ` ` ` `int` `sequence[] = {8, 11, 16, 23}; ` ` ` `int` `N = ` `sizeof` `(sequence) / ` `sizeof` `(` `int` `); ` ` ` ` ` `int` `terms = 3; ` ` ` `nextTermsInSequence(sequence, N, terms); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to generate next terms ` `# of a given polynomial sequence ` ` ` `# Method to print next terms term of sequence ` `def` `nextTermsInSequence(sequence, N, terms): ` ` ` ` ` `diff ` `=` `[` `0` `] ` `*` `(N ` `+` `terms) ` ` ` ` ` `# First copy the sequence itself ` ` ` `# into diff array ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `diff[i] ` `=` `sequence[i] ` ` ` ` ` `more ` `=` `False` ` ` `first ` `=` `[] ` ` ` `length ` `=` `N ` ` ` ` ` `# Loop untill one difference remains ` ` ` `# or all difference become constant ` ` ` `while` `(length > ` `1` `): ` ` ` ` ` `# Keeping the first term of sequence ` ` ` `# for later rebuilding ` ` ` `first.append(diff[` `0` `]) ` ` ` `length ` `-` `=` `1` ` ` ` ` `# Converting the difference to difference ` ` ` `# of differences ` ` ` `for` `i ` `in` `range` `(length): ` ` ` `diff[i] ` `=` `diff[i ` `+` `1` `] ` `-` `diff[i] ` ` ` ` ` `# Checking if all difference values are ` ` ` `# same or not ` ` ` `for` `i ` `in` `range` `(` `1` `, length): ` ` ` `if` `(diff[i] !` `=` `diff[i ` `-` `1` `]): ` ` ` `break` ` ` ` ` `# If some difference values ` ` ` `# were not same ` ` ` `if` `(i !` `=` `length): ` ` ` `break` ` ` ` ` `iteration ` `=` `N ` `-` `length ` ` ` ` ` `# Padding terms instance of constant ` ` ` `# difference at the end of array ` ` ` `for` `i ` `in` `range` `(length, length ` `+` `terms): ` ` ` `diff[i] ` `=` `diff[i ` `-` `1` `] ` ` ` ` ` `length ` `+` `=` `terms ` ` ` ` ` `# Iterating to get actual sequence back ` ` ` `for` `i ` `in` `range` `(iteration): ` ` ` `length ` `+` `=` `1` ` ` ` ` `# Shifting all difference by one place ` ` ` `for` `j ` `in` `range` `(length ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `diff[j] ` `=` `diff[j ` `-` `1` `] ` ` ` ` ` `# Copying actual first element ` ` ` `diff[` `0` `] ` `=` `first[` `len` `(first) ` `-` `i ` `-` `1` `] ` ` ` ` ` `# Converting difference of differences to ` ` ` `# difference array ` ` ` `for` `j ` `in` `range` `(` `1` `, length): ` ` ` `diff[j] ` `=` `diff[j ` `-` `1` `] ` `+` `diff[j] ` ` ` ` ` `# Printing the result ` ` ` `for` `i ` `in` `range` `(length): ` ` ` `print` `(diff[i], end ` `=` `" "` `) ` ` ` ` ` `print` `() ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `sequence ` `=` `[ ` `8` `, ` `11` `, ` `16` `, ` `23` `] ` ` ` `N ` `=` `len` `(sequence) ` ` ` `terms ` `=` `3` ` ` ` ` `nextTermsInSequence(sequence, N, terms) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

8 11 16 23 30 37 44

This article is contributed by **Utkarsh Trivedi**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Finding nth term of any Polynomial Sequence
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Find the Kth smallest element in the sorted generated array
- Find determinant of matrix generated by array rotation
- Find the value at kth position in the generated array
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Find the element in the matrix generated by given rules
- Find the sum of elements of the Matrix generated by the given rules
- Nth term of a recurrence relation generated by two given arrays
- Maximize sum of pairwise products generated from the given Arrays
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Horner's Method for Polynomial Evaluation
- Sgn value of a polynomial
- Program for Derivative of a Polynomial
- Minimize the sum of roots of a given polynomial
- Python | Finding Solutions of a Polynomial Equation
- Integration in a Polynomial for a given value
- Program to find the indefinite Integration of the given Polynomial
- Program to differentiate the given Polynomial