Represent K as sum of N-bonacci numbers

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(2N)



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:

  1. Generate the N-bonacci series and store the values in an array, say N_bonacci[]. (upto 50 terms)
  2. Initialize another array, ans[], for saving the numbers from the series whose sum is K.
  3. 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].
  4. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.