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.
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.
8 11 16 23 32 43 56
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 email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Finding nth term of any Polynomial Sequence
- Find the value at kth position in the generated array
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Find the element in the matrix generated by given rules
- Find the sum of elements of the Matrix generated by the given rules
- Find determinant of matrix generated by array rotation
- Find the Kth smallest element in the sorted generated array
- Sgn value of a polynomial
- Integration in a Polynomial for a given value
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Program for Derivative of a Polynomial
- Minimize the sum of roots of a given polynomial
- Program to differentiate the given Polynomial
- Horner's Method for Polynomial Evaluation
- Program to find the indefinite Integration of the given Polynomial
- Python | Finding Solutions of a Polynomial Equation
- Proof that Independent Set in Graph theory is NP Complete
- Program to find the time remaining for the day to complete
- Minimum time required to complete a work by N persons together
- Program to find total number of edges in a Complete Graph