Print the sequence of size N in which every term is sum of previous K terms
Given two integers N and K, the task is to generate a series of N terms in which every term is sum of the previous K terms.
Note: First term of the series is 1 and if there are not enough previous terms then other terms are suppossed to be 0.
Examples:
Input: N = 8, K = 3
Output: 1 1 2 4 7 13 24 44
Explanation:
Series is generated as follows:
a[0] = 1
a[1] = 1 + 0 + 0 = 1
a[2] = 1 + 1 + 0 = 2
a[3] = 2 + 1 + 1 = 4
a[4] = 4 + 2 + 1 = 7
a[5] = 7 + 4 + 2 = 13
a[6] = 13 + 7 + 4 = 24
a[7] = 24 + 13 + 7 = 44Input: N = 10, K = 4
Output: 1 1 2 4 8 15 29 56 108 208
Naive Approach: The idea is to run two loops to generate N terms of series. Below is the illustration of the steps:
- Traverse the first loop from 0 to N – 1, to generate every term of the series.
- Run a loop from max(0, i – K) to i to compute the sum of the previous K terms.
- Update the sum to the current index of series as the current term.
Below is the implmentation of the above approach:
C++
// C++ implementation to find the // series in which every term is // sum of previous K terms #include <iostream> using namespace std; // Function to generate the // series in the form of array void sumOfPrevK(int N, int K) { int arr[N]; arr[0] = 1; // Pick a starting point for (int i = 1; i < N; i++) { int j = i - 1, count = 0, sum = 0; // Find the sum of all // elements till count < K while (j >= 0 && count < K) { sum += arr[j]; j--; count++; } // Find the value of // sum at i position arr[i] = sum; } for (int i = 0; i < N; i++) { cout << arr[i] << " "; } } // Driver Code int main() { int N = 10, K = 4; sumOfPrevK(N, K); return 0; } |
Java
// Java implementation to find the // series in which every term is // sum of previous K terms class Sum { // Function to generate the // series in the form of array void sumOfPrevK(int N, int K) { int arr[] = new int[N]; arr[0] = 1; // Pick a starting point for (int i = 1; i < N; i++) { int j = i - 1, count = 0, sum = 0; // Find the sum of all // elements till count < K while (j >= 0 && count < K) { sum += arr[j]; j--; count++; } // Find the value of // sum at i position arr[i] = sum; } for (int i = 0; i < N; i++) { System.out.print(arr[i] + " "); } } // Driver Code public static void main(String args[]) { Sum s = new Sum(); int N = 10, K = 4; s.sumOfPrevK(N, K); } } |
Python3
# Python3 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 def sumOfPrevK(N, K): arr = [0 for i in range(N)] arr[0] = 1 # Pick a starting point for i in range(1,N): j = i - 1 count = 0 sum = 0 # Find the sum of all # elements till count < K while (j >= 0 and count < K): sum = sum + arr[j] j = j - 1 count = count + 1 # Find the value of # sum at i position arr[i] = sum for i in range(0, N): print(arr[i]) # Driver Code N = 10K = 4sumOfPrevK(N, K) # This code is contributed by Sanjit_Prasad |
C#
// C# implementation to find the // series in which every term is // sum of previous K terms using System; class Sum { // Function to generate the // series in the form of array void sumOfPrevK(int N, int K) { int []arr = new int[N]; arr[0] = 1; // Pick a starting point for (int i = 1; i < N; i++) { int j = i - 1, count = 0, sum = 0; // Find the sum of all // elements till count < K while (j >= 0 && count < K) { sum += arr[j]; j--; count++; } // Find the value of // sum at i position arr[i] = sum; } for (int i = 0; i < N; i++) { Console.Write(arr[i] + " "); } } // Driver Code public static void Main(String []args) { Sum s = new Sum(); int N = 10, K = 4; s.sumOfPrevK(N, K); } } // This code is contributed by 29AjayKumar |
Performance analysis:
- Time Complexity: O(N * K)
- Space Complexity: O(N)
Efficient Approach: The idea is to store the current sum in a variable and in every step subtract the last Kth term and add the last term into the pre-sum to compute every term of the series.
Below is the implementation of the above approach:
C++
// C++ implementation to find the // series in which every term is // sum of previous K terms #include <iostream> using namespace std; // Function to generate the // series in the form of array void sumOfPrevK(int N, int K) { int arr[N], prevsum = 0; arr[0] = 1; // Pick a starting point for (int i = 0; i < N - 1; i++) { // Computing the previous sum 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] + prevsum; } } // Loop to print the series for (int i = 0; i < N; i++) { cout << arr[i] << " "; } } // Driver Code int main() { int N = 8, K = 3; sumOfPrevK(N, K); return 0; } |
Java
// Java implementation to find the // series in which every term is // sum of previous K terms class Sum { // Function to generate the // series in the form of array void sumOfPrevK(int N, int K) { int arr[] = new int[N]; int prevsum = 0; arr[0] = 1; // Pick a starting point for (int i = 0; i < N - 1; i++) { // Computing the previous sum 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] + prevsum; } } // Loop to print the series for (int i = 0; i < N; i++) { System.out.print(arr[i] + " "); } } // Driver code public static void main(String args[]) { Sum s = new Sum(); int N = 8, K = 3; s.sumOfPrevK(N, K); } } |
Complexity analysis:
- Time Complexity: O(N)
- Space Complexity: O(N)
Recommended Posts:
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Print N terms of Withoff Sequence
- Print first N terms of Lower Wythoff sequence
- Print n terms of Newman-Conway Sequence
- Find Pth term of a GP if Mth and Nth terms are given
- Sum of first N terms of Quadratic Sequence 3 + 7 + 13 + ...
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Finding nth term of any Polynomial Sequence
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Program to find Nth term of the Van Eck's Sequence
- Find nth term of the Dragon Curve Sequence
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Program to print the series 1, 9, 17, 33, 49, 73, 97... till N terms
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Program to print the sum of the given nth term
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

