Given two numbers K and N. The task is to represent the given number K as a sum of several N-bonacci numbers.
Input: K = 21, N = 5
The three numbers of the 5-bonacci numbers are: 16, 4, 1.
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
The numbers of 43-bonacci that add up to 500 are: 256 128 64 32 16 4
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:
- 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:
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.
- Represent a number as sum of minimum possible psuedobinary numbers
- Number of ways to represent a number as sum of k fibonacci numbers
- Represent the given number as the sum of two composite numbers
- Represent K^N as the sum of exactly N numbers
- Represent N as sum of K odd numbers with repetitions allowed
- Represent N as sum of K even numbers
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Represent a number as the sum of positive numbers ending with 9
- 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
- Ways to represent a number as a sum of 1's and 2's
- Represent n as the sum of exactly k powers of two | Set 2
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Number of distinct ways to represent a number as sum of K unique primes
- Add two integers of different base and represent sum in smaller base of the two
- Count of ways to represent N as sum of a prime number and twice of a square
- Represent a given set of points by the best possible straight line
- Count of sticks required to represent the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.