Print the sequence of size N in which every term is sum of previous K terms | C ++ implementation to find the series in which every term is sum of previous K terms ; Function to generate the series in the form of array ; Pick a starting point ; Computing the previous sum ; Loop to print the series ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void sumOfPrevK ( int N , int K ) { int arr [ N ] , prevsum = 0 ; arr [ 0 ] = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( i < K ) { arr [ i + 1 ] = arr [ i ] + prevsum ; prevsum = arr [ i + 1 ] ; } else { arr [ i + 1 ] = arr [ i ] + prevsum - arr [ i + 1 - K ] ; prevsum = arr [ i + 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int N = 8 , K = 3 ; sumOfPrevK ( N , K ) ; return 0 ; }