Given a number k, find the required minimum number of Fibonacci terms whose sum equal to k. We can choose a Fibonacci number multiple times.
Input : k = 4 Output : 2 Fibonacci term added twice that is 2 + 2 = 4. Other combinations are 1 + 1 + 2. 1 + 1 + 1 + 1 Among all cases first case has the minimum number of terms = 2. Input : k = 17 Output : 3
We can get any sum using Fibonacci numbers as 1 is a Fibonacci number. For example, to get n, we can n times add 1. Here we need to minimize the count of Fibonacci numbers that contribute to sum. So this problem is basically coin change problem with coins having Fibonacci values. By taking some examples, we can notice that With Fibonacci coin values Greedy approach works.
Firstly we calculate Fibonacci terms till less than or equal to k. then start from the last term and keep subtracting that term from k until k >(nth term). Also along with this keep increasing the count of the number of terms.
When k < (nth Fibonacci term) move to next Fibonacci term which is less than or Equal to k. at last, print the value of count.
The stepwise algorithm is:
1. Find all Fibonacci Terms less than or equal to K. 2. Initialize count = 0. 3. j = Index of last calculated Fibonacci Term. 4. while K > 0 do: // Greedy step count += K / (fibo[j]) // Note that division // is repeated subtraction. K = K % (fibo[j]) j--; 5. Print count.
Below is the implementation of the above approach.
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.
- Find n terms of Fibonacci type series with given first two terms
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Sum of Fibonacci numbers at even indexes upto N terms
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Check if a M-th fibonacci number divides N-th fibonacci number
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Minimum number of Fibonacci jumps to reach end
- Sum of Fibonacci Numbers
- Even Fibonacci Numbers Sum
- Fibonacci sum of a subset with all elements <= k
- Number of ways to represent a number as sum of k fibonacci numbers
- Sum of squares of Fibonacci numbers
- Sum of Fibonacci Numbers with alternate negatives
- Find the sum of first N odd Fibonacci numbers
- Sum of Fibonacci Numbers in a range
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Sum of numbers in the Kth level of a Fibonacci triangle
- Find two Fibonacci numbers whose sum can be represented as N
- Count of Fibonacci pairs with sum N in range 0 to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.