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 ; Find the sum of all elements till count < K ; Find the value of sum at i position ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void sumOfPrevK ( int N , int K ) { int arr [ N ] ; arr [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { int j = i - 1 , count = 0 , sum = 0 ; while ( j >= 0 && count < K ) { sum += arr [ j ] ; j -- ; count ++ ; } arr [ i ] = sum ; } for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int N = 10 , K = 4 ; sumOfPrevK ( N , K ) ; return 0 ; }