Print the sequence of size N 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
function sumOfPrevK ( N , K ) { let arr = new Array ( N ) ; arr [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) { let j = i - 1 , count = 0 , sum = 0 ; while ( j >= 0 && count < K ) { sum += arr [ j ] ; j -- ; count ++ ; } arr [ i ] = sum ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let N = 10 , K = 4 ; sumOfPrevK ( N , K ) ;