Given two numbers **K** and **N**. The task is to represent the given number **K** as a sum of several **N-bonacci numbers**.

**Examples:**

Input:K = 21, N = 5

Output:3

The three numbers of the 5-bonacci numbers are: 16, 4, 1.

Explanation:

For N = 5, the series will be: 1, 1, 2, 4, 8, 16, 31, 61, 120, and so on. The three numbers 16, 4 and 1 add up to 21.

Input:K = 500, N = 43

Output:6

The numbers of 43-bonacci that add up to 500 are: 256 128 64 32 16 4

**Naive Approach:**

The simplest approach is to generate the N-bonacci series upto 50 terms and store their values in an array. Find the subset of the array which has the given sum and print the size of the subset.

**Time Complexity:** O(2^{N})

**Efficient Approach**: This approach is based on the efficient approach on how to form N-bonacci numbers, discussed in this article.

Follow the steps below to solve the problem:

- Generate the N-bonacci series and store the values in an array, say
**N_bonacci[]**. (upto 50 terms) - Initialize another array,
**ans[]**, for saving the numbers from the series whose sum is**K**. - Traverse the N-bonacci array in the reverse order and keep checking if,
**K – N_bonacci[i] >= 0**. If true, then push the**N_bonacci[i]**to the**ans**array, and reduce**K**to**K – N_bonacci[i]**. - Continue decrementing
**K**till it becomes**0**, and then finally output the size and the elements of**ans**array.

Below is the implementation of the above approach:

`// CPP program for the above problem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// array to store the N-Bonacci series ` `long` `long` `N_bonacci[100]; ` ` ` `// Function to express K as sum of ` `// several N_bonacci numbers ` `void` `N_bonacci_nums(` `int` `n, ` `int` `k) ` `{ ` ` ` `N_bonacci[0] = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i <= 50; ++i) { ` ` ` `for` `(` `int` `j = i - 1; ` ` ` `j >= i - k and j >= 0; ` ` ` `--j) ` ` ` `N_bonacci[i] ` ` ` `+= N_bonacci[j]; ` ` ` `} ` ` ` ` ` `vector<` `long` `long` `> ans; ` ` ` `for` `(` `int` `i = 50; i >= 0; --i) ` ` ` `if` `(n - N_bonacci[i] >= 0) { ` ` ` `ans.push_back(N_bonacci[i]); ` ` ` `n -= N_bonacci[i]; ` ` ` `} ` ` ` ` ` `if` `(ans.size() == 1) ` ` ` `ans.push_back(0); ` ` ` ` ` `cout << ans.size() << endl; ` ` ` `for` `(` `int` `i = 0; i < ans.size(); ++i) ` ` ` `cout << ans[i] << ` `", "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 21, k = 5; ` ` ` `N_bonacci_nums(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3 16, 4, 1,

**Time Complexity:** O(K * K)

**Auxillary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Represent K^N as the sum of exactly N numbers
- Represent N as sum of K even numbers
- Represent N as sum of K odd numbers with repetitions allowed
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Represent the given number as the sum of two composite numbers
- Represent a number as sum of minimum possible psuedobinary numbers
- Represent the fraction of two numbers in the string format
- Minimum count of numbers required from given array to represent S
- Sort the strings based on the numbers of matchsticks required to represent them
- Sort numbers based on count of letters required to represent them in words
- Number of ways to represent a number as sum of k fibonacci numbers
- Represent n as the sum of exactly k powers of two | Set 2
- Ways to represent a number as a sum of 1's and 2's
- Represent a given set of points by the best possible straight line
- Count of sticks required to represent the given string
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Count of matchsticks required to represent the given number
- Check given array of size n can represent BST of n levels or not
- Count of integers up to N which represent a Binary number
- Count of ways to represent N as sum of a prime number and twice of a square

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.