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 the below code for a better understanding.
C++
#include <bits/stdc++.h>
using namespace std;
void nextTermsInSequence( int sequence[], int N, int terms)
{
int diff[N + terms];
for ( int i = 0; i < N; i++)
diff[i] = sequence[i];
bool more = false ;
vector< int > first;
int len = N;
while (len > 1)
{
first.push_back(diff[0]);
len--;
for ( int i = 0; i < len; i++)
diff[i] = diff[i + 1] - diff[i];
int i;
for (i = 1; i < len; i++)
if (diff[i] != diff[i - 1])
break ;
if (i != len)
break ;
}
int iteration = N - len;
for ( int i = len; i < len + terms; i++)
diff[i] = diff[i - 1];
len += terms;
for ( int i = 0; i < iteration; i++)
{
len++;
for ( int j = len - 1; j > 0; j--)
diff[j] = diff[j - 1];
diff[0] = first[first.size() - i - 1];
for ( int j = 1; j < len; j++)
diff[j] = diff[j - 1] + diff[j];
}
for ( int i = 0; i < len; i++)
cout << diff[i] << " " ;
cout << endl;
}
int main()
{
int sequence[] = {8, 11, 16, 23};
int N = sizeof (sequence) / sizeof ( int );
int terms = 3;
nextTermsInSequence(sequence, N, terms);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void nextTermsInSequence( int []sequence,
int N, int terms)
{
int []diff = new int [N + terms];
for ( int i = 0 ; i < N; i++)
diff[i] = sequence[i];
ArrayList<Object> first = new ArrayList<Object>();
int len = N;
while (len > 1 )
{
first.add(diff[ 0 ]);
len--;
for ( int i = 0 ; i < len; i++)
diff[i] = diff[i + 1 ] - diff[i];
int j;
for (j = 1 ; j < len; j++)
if (diff[j] != diff[j - 1 ])
break ;
if (j != len)
break ;
}
int iteration = N - len;
for ( int i = len; i < len + terms; i++)
diff[i] = diff[i - 1 ];
len += terms;
for ( int i = 0 ; i < iteration; i++)
{
len++;
for ( int j = len - 1 ; j > 0 ; j--)
diff[j] = diff[j - 1 ];
diff[ 0 ] = ( int )first.get(first.size() - i - 1 );
for ( int j = 1 ; j < len; j++)
diff[j] = diff[j - 1 ] + diff[j];
}
for ( int i = 0 ; i < len; i++)
{
System.out.print(diff[i] + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int []sequence = { 8 , 11 , 16 , 23 };
int N = sequence.length;
int terms = 3 ;
nextTermsInSequence(sequence, N, terms);
}
}
|
Python3
def nextTermsInSequence(sequence, N, terms):
diff = [ 0 ] * (N + terms)
for i in range (N):
diff[i] = sequence[i]
more = False
first = []
length = N
while (length > 1 ):
first.append(diff[ 0 ])
length - = 1
for i in range (length):
diff[i] = diff[i + 1 ] - diff[i]
for i in range ( 1 , length):
if (diff[i] ! = diff[i - 1 ]):
break
if (i ! = length):
break
iteration = N - length
for i in range (length, length + terms):
diff[i] = diff[i - 1 ]
length + = terms
for i in range (iteration):
length + = 1
for j in range (length - 1 , - 1 , - 1 ):
diff[j] = diff[j - 1 ]
diff[ 0 ] = first[ len (first) - i - 1 ]
for j in range ( 1 , length):
diff[j] = diff[j - 1 ] + diff[j]
for i in range (length):
print (diff[i], end = " " )
print ()
if __name__ = = "__main__" :
sequence = [ 8 , 11 , 16 , 23 ]
N = len (sequence)
terms = 3
nextTermsInSequence(sequence, N, terms)
|
C#
using System;
using System.Collections;
class GFG{
static void nextTermsInSequence( int []sequence,
int N, int terms)
{
int []diff = new int [N + terms];
for ( int i = 0; i < N; i++)
diff[i] = sequence[i];
ArrayList first = new ArrayList();
int len = N;
while (len > 1)
{
first.Add(diff[0]);
len--;
for ( int i = 0; i < len; i++)
diff[i] = diff[i + 1] - diff[i];
int j;
for (j = 1; j < len; j++)
if (diff[j] != diff[j - 1])
break ;
if (j != len)
break ;
}
int iteration = N - len;
for ( int i = len; i < len + terms; i++)
diff[i] = diff[i - 1];
len += terms;
for ( int i = 0; i < iteration; i++)
{
len++;
for ( int j = len - 1; j > 0; j--)
diff[j] = diff[j - 1];
diff[0] = ( int )first[first.Count - i - 1];
for ( int j = 1; j < len; j++)
diff[j] = diff[j - 1] + diff[j];
}
for ( int i = 0; i < len; i++)
{
Console.Write(diff[i] + " " );
}
Console.Write( "\n" );
}
public static void Main( string [] args)
{
int []sequence = { 8, 11, 16, 23 };
int N = sequence.Length;
int terms = 3;
nextTermsInSequence(sequence, N, terms);
}
}
|
Javascript
<script>
function nextTermsInSequence(sequence, N,terms)
{
let diff = new Array(N + terms);
for (let i = 0; i < N; i++)
diff[i] = sequence[i];
let first=[];
let len = N;
while (len > 1)
{
first.push(diff[0]);
len--;
for (let i = 0; i < len; i++)
diff[i] = diff[i + 1] - diff[i];
let j;
for (j = 1; j < len; j++)
if (diff[j] != diff[j - 1])
break ;
if (j != len)
break ;
}
let iteration = N - len;
for (let i = len; i < len + terms; i++)
diff[i] = diff[i - 1];
len += terms;
for (let i = 0; i < iteration; i++)
{
len++;
for (let j = len - 1; j > 0; j--)
diff[j] = diff[j - 1];
diff[0] = first[first.length - i - 1];
for (let j = 1; j < len; j++)
diff[j] = diff[j - 1] + diff[j];
}
for (let i = 0; i < len; i++)
{
document.write(diff[i] + " " );
}
document.write( "<br>" );
}
let sequence=[8, 11, 16, 23];
let N = sequence.length;
let terms = 3;
nextTermsInSequence(sequence, N, terms);
</script>
|
Output8 11 16 23 30 37 44
Time Complexity: O(N2+N*terms)
Auxiliary Space: O(N+terms)
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.