# K- Fibonacci series

Given integers ‘K’ and ‘N’, the task is to find the Nth term of the K-Fibonacci series.

In K – Fibonacci series, the first ‘K’ terms will be ‘1’ and after that every `ith` term of the series will be the sum of previous ‘K’ elements in the same series.

Examples:

```Input: N = 4, K = 2
Output: 3
The K-Fibonacci series for K=2 is 1, 1, 2, 3, ...
And, the 4th element is 3.

Input: N = 5, K = 6
Output: 1
The K-Fibonacci series for K=6 is 1, 1, 1, 1, 1, 1, 6, 11, ...
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple approach:

• First, initialize the first ‘K’ elements to ‘1’.
• Then, calculate the sum of previous ‘K’ elements by running a loop from ‘`i-k`‘ to ‘`i-1`‘.
• Set the ith value to the sum.

Time Complexity: O(N*K)

An efficient approach:

• First, initialize the first ‘K’ elements to ‘1’.
• Create a variable named ‘sum’ which will be initialized with ‘K’.
• Set the value of `(K+1)th` element to sum.
• Set the next values as `Array[i] = sum - Array[i-k-1] + Array[i-1]` then update `sum = Array[i]`.
• In the end, display the Nth term of the array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that finds the Nth ` `// element of K-Fibonacci series ` `void` `solve(``int` `N, ``int` `K) ` `{ ` `    ``vector<``long` `long` `int``> Array(N + 1, 0); ` ` `  `    ``// If N is less than K ` `    ``// then the element is '1' ` `    ``if` `(N <= K) { ` `        ``cout << ``"1"` `<< endl; ` `        ``return``; ` `    ``} ` ` `  `    ``long` `long` `int` `i = 0, sum = K; ` ` `  `    ``// first k elements are 1 ` `    ``for` `(i = 1; i <= K; ++i) { ` `        ``Array[i] = 1; ` `    ``} ` ` `  `    ``// (K+1)th element is K ` `    ``Array[i] = sum; ` ` `  `    ``// find the elements of the ` `    ``// K-Fibonacci series ` `    ``for` `(``int` `i = K + 2; i <= N; ++i) { ` ` `  `        ``// subtract the element at index i-k-1 ` `        ``// and add the element at index i-i ` `        ``// from the sum (sum contains the sum ` `        ``// of previous 'K' elements ) ` `        ``Array[i] = sum - Array[i - K - 1] + Array[i - 1]; ` ` `  `        ``// set the new sum ` `        ``sum = Array[i]; ` `    ``} ` `    ``cout << Array[N] << endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``long` `long` `int` `N = 4, K = 2; ` ` `  `    ``// get the Nth value ` `    ``// of K-Fibonacci series ` `    ``solve(N, K); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` ` `  `public` `class` `GFG { ` ` `  `    ``// Function that finds the Nth ` `    ``// element of K-Fibonacci series ` `    ``static` `void` `solve(``int` `N, ``int` `K) ` `    ``{ ` `        ``int` `Array[] = ``new` `int``[N + ``1``]; ` `         `  ` `  `        ``// If N is less than K ` `        ``// then the element is '1' ` `        ``if` `(N <= K) { ` `            ``System.out.println(``"1"``) ; ` `            ``return``; ` `        ``} ` ` `  `        ``int` `i = ``0` `;  ` `        ``int` `sum = K; ` ` `  `        ``// first k elements are 1 ` `        ``for` `(i = ``1``; i <= K; ++i) { ` `            ``Array[i] = ``1``; ` `        ``} ` ` `  `        ``// (K+1)th element is K ` `        ``Array[i] = sum; ` ` `  `        ``// find the elements of the ` `        ``// K-Fibonacci series ` `        ``for` `(i = K + ``2``; i <= N; ++i) { ` ` `  `            ``// subtract the element at index i-k-1 ` `            ``// and add the element at index i-i ` `            ``// from the sum (sum contains the sum ` `            ``// of previous 'K' elements ) ` `            ``Array[i] = sum - Array[i - K - ``1``] + Array[i - ``1``]; ` ` `  `            ``// set the new sum ` `            ``sum = Array[i]; ` `        ``} ` `        ``System.out.println(Array[N]); ` `    ``} ` ` `  `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `          ``int` `N = ``4``, K = ``2``; ` ` `  `            ``// get the Nth value ` `            ``// of K-Fibonacci series ` `            ``solve(N, K); ` ` `  `    ``} ` `    ``// This code is contributed by ANKITRAI1 ` `} `

## Python3

 `# Python3 implementation of above approach ` ` `  `# Function that finds the Nth ` `# element of K-Fibonacci series ` `def` `solve(N, K) : ` `    ``Array ``=` `[``0``] ``*` `(N ``+` `1``) ` `     `  `    ``# If N is less than K ` `    ``# then the element is '1' ` `    ``if` `(N <``=` `K) : ` `        ``print``(``"1"``) ` `        ``return` `     `  `    ``i ``=` `0` `    ``sm ``=` `K ` `     `  `    ``# first k elements are 1 ` `    ``for` `i ``in` `range``(``1``, K ``+` `1``) : ` `        ``Array[i] ``=` `1` `         `  `    ``# (K+1)th element is K ` `    ``Array[i ``+` `1``] ``=` `sm ` `     `  `    ``# find the elements of the ` `    ``# K-Fibonacci series ` `    ``for` `i ``in` `range``(K ``+` `2``, N ``+` `1``) : ` `         `  `        ``# subtract the element at index i-k-1 ` `        ``# and add the element at index i-i ` `        ``# from the sum (sum contains the sum ` `        ``# of previous 'K' elements ) ` `        ``Array[i] ``=` `sm ``-` `Array[i ``-` `K ``-` `1``] ``+` `Array[i ``-` `1``] ` ` `  `        ``# set the new sum ` `        ``sm ``=` `Array[i] ` ` `  `    ``print``(Array[N]) ` `     `  `     `  `# Driver code ` `N ``=` `4` `K ``=` `2` ` `  `# get the Nth value ` `# of K-Fibonacci series ` `solve(N, K) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# implementation of above approach  ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function that finds the Nth  ` `    ``// element of K-Fibonacci series  ` `    ``public` `static` `void` `solve(``int` `N, ``int` `K) ` `    ``{ ` `        ``int``[] Array = ``new` `int``[N + 1]; ` ` `  ` `  `        ``// If N is less than K  ` `        ``// then the element is '1'  ` `        ``if` `(N <= K) ` `        ``{ ` `            ``Console.WriteLine(``"1"``); ` `            ``return``; ` `        ``} ` ` `  `        ``int` `i = 0; ` `        ``int` `sum = K; ` ` `  `        ``// first k elements are 1  ` `        ``for` `(i = 1; i <= K; ++i) ` `        ``{ ` `            ``Array[i] = 1; ` `        ``} ` ` `  `        ``// (K+1)th element is K  ` `        ``Array[i] = sum; ` ` `  `        ``// find the elements of the  ` `        ``// K-Fibonacci series  ` `        ``for` `(i = K + 2; i <= N; ++i) ` `        ``{ ` ` `  `            ``// subtract the element at index i-k-1  ` `            ``// and add the element at index i-i  ` `            ``// from the sum (sum contains the sum  ` `            ``// of previous 'K' elements )  ` `            ``Array[i] = sum - Array[i - K - 1] + ` `                                 ``Array[i - 1]; ` ` `  `            ``// set the new sum  ` `            ``sum = Array[i]; ` `        ``} ` `        ``Console.WriteLine(Array[N]); ` `    ``} ` ` `  `    ``// Main Method ` `    ``public` `static` `void` `Main(``string``[] args) ` `    ``{ ` `        ``int` `N = 4, K = 2; ` ` `  `            ``// get the Nth value  ` `            ``// of K-Fibonacci series  ` `            ``solve(N, K); ` ` `  `    ``} ` `     `  `} ` ` `  `// This code is contributed ` `// by Shrikant13 `

## PHP

 ` `

Output:

```3
```

Time Complexity: O(N)

My Personal Notes arrow_drop_up Second year Department of Information Technology Jadavpur University

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.