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.
Examples:
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.
C++
#include <bits/stdc++.h>
using namespace std;
void calcFiboTerms(vector< int >& fiboTerms, int K)
{
int i = 3, nextTerm;
fiboTerms.push_back(0);
fiboTerms.push_back(1);
fiboTerms.push_back(1);
while (1) {
nextTerm = fiboTerms[i - 1] + fiboTerms[i - 2];
if (nextTerm > K)
return ;
fiboTerms.push_back(nextTerm);
i++;
}
}
int findMinTerms( int K)
{
vector< int > fiboTerms;
calcFiboTerms(fiboTerms, K);
int count = 0, j = fiboTerms.size() - 1;
while (K > 0) {
count += (K / fiboTerms[j]);
K %= (fiboTerms[j]);
j--;
}
return count;
}
int main()
{
int K = 17;
cout << findMinTerms(K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void calcFiboTerms(ArrayList<Integer> fiboterms,
int k)
{
int i = 3 , nextTerm = 0 ;
fiboterms.add( 0 );
fiboterms.add( 1 );
fiboterms.add( 1 );
while ( true )
{
nextTerm = fiboterms.get(i - 1 ) + fiboterms.get(i - 2 );
if (nextTerm>k)
return ;
fiboterms.add(nextTerm);
i++;
}
}
public static int fibMinTerms( int k)
{
ArrayList<Integer> fiboterms = new ArrayList<Integer>();
calcFiboTerms(fiboterms,k);
int count = 0 , j = fiboterms.size() - 1 ;
while (k > 0 )
{
count += (k / fiboterms.get(j));
k %= (fiboterms.get(j));
j--;
}
return count;
}
public static void main (String[] args) {
int k = 17 ;
System.out.println(fibMinTerms(k));
}
}
|
Python3
def calcFiboTerms(fiboTerms, K):
i = 3
fiboTerms.append( 0 )
fiboTerms.append( 1 )
fiboTerms.append( 1 )
while True :
nextTerm = (fiboTerms[i - 1 ] +
fiboTerms[i - 2 ])
if nextTerm > K:
return
fiboTerms.append(nextTerm)
i + = 1
def findMinTerms(K):
fiboTerms = []
calcFiboTerms(fiboTerms, K)
count, j = 0 , len (fiboTerms) - 1
while K > 0 :
count + = K / / fiboTerms[j]
K % = fiboTerms[j]
j - = 1
return count
if __name__ = = "__main__" :
K = 17
print (findMinTerms(K))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void calcFiboTerms(List< int > fiboterms,
int k)
{
int i = 3, nextTerm = 0;
fiboterms.Add(0);
fiboterms.Add(1);
fiboterms.Add(1);
while ( true )
{
nextTerm = fiboterms[i - 1] +
fiboterms[i - 2];
if (nextTerm > k)
return ;
fiboterms.Add(nextTerm);
i++;
}
}
public static int fibMinTerms( int k)
{
List< int > fiboterms = new List< int >();
calcFiboTerms(fiboterms, k);
int count = 0, j = fiboterms.Count - 1;
while (k > 0)
{
count += (k / fiboterms[j]);
k %= (fiboterms[j]);
j--;
}
return count;
}
public static void Main (String[] args)
{
int k = 17;
Console.WriteLine(fibMinTerms(k));
}
}
|
Javascript
<script>
function calcFiboTerms(fiboTerms, K)
{
var i = 3, nextTerm;
fiboTerms.push(0);
fiboTerms.push(1);
fiboTerms.push(1);
while (1)
{
nextTerm = fiboTerms[i - 1] +
fiboTerms[i - 2];
if (nextTerm > K)
return ;
fiboTerms.push(nextTerm);
i++;
}
}
function findMinTerms(K)
{
var fiboTerms = [];
calcFiboTerms(fiboTerms, K);
var count = 0;
var j = fiboTerms.length - 1;
while (K > 0)
{
count += parseInt(K / fiboTerms[j]);
K %= (fiboTerms[j]);
j--;
}
return count;
}
var K = 17;
document.write(findMinTerms(K));
</script>
|
Time Complexity: O(k), where k represents the given input.
Auxiliary Space: O(k), where k represents the given input.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!