Given a number N, the task is to find the number of ways in which the integer N can be represented as a sum of Fibonacci numbers without repetition of any Fibonacci number.
Examples:
Input: N = 13
Output: 3
Explanation:
The possible ways to select N as 13 are: {13} {8, 5} {8, 3, 2}. Note that it is not possible to select {5 + 5 + 3} because 5 appears twice.
Input: N = 87
Output: 5
Explanation:
The possible ways to select N as 13 are: {55 + 21 + 8 + 3}, {55 + 21 + 8 + 2 + 1}, {55 + 21 + 5 + 3 + 2 + 1}, {55 + 13 + 8 + 5 + 3 + 2 + 1}, {34 + 21 + 13 + 8 + 5 + 3 + 2 + 1}.
Naive Approach: The naive idea is to write all the possible combinations that add up to given number N. Check if any combination has repeated integers then don’t increase the counter otherwise increase the count by 1 each time. Return the count at the end.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use Dynamic Programming to optimize the above approach. Below are the steps:
Imagine Fibonacci coding by following way: the i-th bit of number corresponds to the i-th Fibonacci number.
For Example: 16 = 13 + 3 will be written as 100100.
- Write Fibonacci Code for every positive number such that no two adjacent bit is 1.
- This is true for all numbers because if there are two adjacent bits are 1-bits then we can transform it into a single 1-bit by property of Fibonacci number. Let’s call this representation as canonical representation.
- Get the Canonical Representation. Generate several Fibonacci numbers (about 90) and after that try to subtract all of them in the decreasing order.
- Let’s store positions of 1-bits of the canonical representation of a given number into an array v in the increasing order and decompose any 1-bits into two 1-bits as follows:
Starting canonical representation: 1000000001
After decomposing leftmost 1-bit into two smaller 1-bits: 0110000001
After decomposing 2’nd leftmost 1-bit into two smaller 1-bits: 0101100001
After decomposing 3’rd leftmost 1-bit into two smaller 1-bits: 0101011001
After decomposing 4’th leftmost 1-bit into two smaller 1-bits: 0101010111
- After a number of such operations, we will get the next 1-bit(or the end of the number). This 1-bit also can be decomposed, but it can be shifted by only one bit.
- Initialize a dp array dp1[], dp1[i] is a number of ways to represent a number that consists of i leftmost 1-bits of the number for the case where all the remaining 1-bits are not decomposed. Also, take dp2[i] which marks the number of ways to represent a number that consists of i leftmost 1-bits of the number for the case where all the remaining 1-bits are decomposed.
For Example: N = 87
Canonical form of N = 101010100
Other 4 possible representations of N are 101010011, 101001111, 100111111, 011111111
Below is the illustration of the same:


Hence, the answer is dp1[cnt] + dp2[cnt], where cnt is the total number of 1-bits in the canonical representation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long fib[101], dp1[101];
long long dp2[101], v[101];
void fibonacci()
{
fib[1] = 1;
fib[2] = 2;
for ( int i = 3; i <= 87; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
int find( int num)
{
int cnt = 0;
for ( int i = 87; i > 0; i--) {
if (num >= fib[i]) {
v[cnt++] = i;
num -= fib[i];
}
}
reverse(v, v + cnt);
dp1[0] = 1;
dp2[0] = (v[0] - 1) / 2;
for ( int i = 1; i < cnt; i++) {
dp1[i] = dp1[i - 1] + dp2[i - 1];
dp2[i] = ((v[i] - v[i - 1]) / 2)
* dp2[i - 1]
+ ((v[i] - v[i - 1] - 1) / 2)
* dp1[i - 1];
}
return (dp1[cnt - 1] + dp2[cnt - 1]);
}
int main()
{
fibonacci();
int num = 13;
cout << find(num);
return 0;
}
|
Java
import java.util.*;
class GFG{
static long [] fib = new long [ 101 ];
static long [] dp1 = new long [ 101 ];
static long [] dp2 = new long [ 101 ];
static long [] v = new long [ 101 ];
static void fibonacci()
{
fib[ 1 ] = 1 ;
fib[ 2 ] = 2 ;
for ( int i = 3 ; i <= 87 ; i++)
{
fib[i] = fib[i - 1 ] + fib[i - 2 ];
}
}
static long find( int num)
{
int cnt = 0 ;
for ( int i = 87 ; i > 0 ; i--)
{
if (num >= fib[i])
{
v[cnt++] = i;
num -= fib[i];
}
}
for ( int i = 0 ; i < cnt / 2 ; i++)
{
long t = v[i];
v[i] = v[cnt - i - 1 ];
v[cnt - i - 1 ] = t;
}
dp1[ 0 ] = 1 ;
dp2[ 0 ] = (v[ 0 ] - 1 ) / 2 ;
for ( int i = 1 ; i < cnt; i++)
{
dp1[i] = dp1[i - 1 ] + dp2[i - 1 ];
dp2[i] = ((v[i] - v[i - 1 ]) / 2 ) *
dp2[i - 1 ] +
((v[i] - v[i - 1 ] - 1 ) / 2 ) *
dp1[i - 1 ];
}
return (dp1[cnt - 1 ] + dp2[cnt - 1 ]);
}
public static void main (String[] args)
{
fibonacci();
int num = 13 ;
System.out.print(find(num));
}
}
|
Python3
fib = [ 0 ] * 101
dp1 = [ 0 ] * 101
dp2 = [ 0 ] * 101
v = [ 0 ] * 101
def fibonacci():
fib[ 1 ] = 1
fib[ 2 ] = 2
for i in range ( 3 , 87 + 1 ):
fib[i] = fib[i - 1 ] + fib[i - 2 ]
def find(num):
cnt = 0
for i in range ( 87 , 0 , - 1 ):
if (num > = fib[i]):
v[cnt] = i
cnt + = 1
num - = fib[i]
v[:: - 1 ]
dp1[ 0 ] = 1
dp2[ 0 ] = (v[ 0 ] - 1 ) / / 2
for i in range ( 1 , cnt):
dp1[i] = dp1[i - 1 ] + dp2[i - 1 ]
dp2[i] = (((v[i] - v[i - 1 ]) / / 2 ) *
dp2[i - 1 ] +
((v[i] - v[i - 1 ] - 1 ) / / 2 ) *
dp1[i - 1 ])
return dp1[cnt - 1 ] + dp2[cnt - 1 ]
fibonacci()
num = 13
print (find(num))
|
C#
using System;
class GFG{
static long [] fib = new long [101];
static long [] dp1 = new long [101];
static long [] dp2 = new long [101];
static long [] v = new long [101];
static void fibonacci()
{
fib[1] = 1;
fib[2] = 2;
for ( int i = 3; i <= 87; i++)
{
fib[i] = fib[i - 1] + fib[i - 2];
}
}
static long find( long num)
{
int cnt = 0;
for ( int i = 87; i > 0; i--)
{
if (num >= fib[i])
{
v[cnt++] = i;
num -= fib[i];
}
}
for ( int i = 0; i < cnt / 2; i++)
{
long t = v[i];
v[i] = v[cnt - i - 1];
v[cnt - i - 1] = t;
}
dp1[0] = 1;
dp2[0] = (v[0] - 1) / 2;
for ( int i = 1; i < cnt; i++)
{
dp1[i] = dp1[i - 1] + dp2[i - 1];
dp2[i] = ((v[i] - v[i - 1]) / 2) *
dp2[i - 1] +
((v[i] - v[i - 1] - 1) / 2) *
dp1[i - 1];
}
return (dp1[cnt - 1] + dp2[cnt - 1]);
}
static void Main()
{
fibonacci();
int num = 13;
Console.Write(find(num));
}
}
|
Javascript
<script>
var fib = Array(101).fill(0);
var dp1 = Array(101).fill(0);
var dp2 = Array(101).fill(0);
var v = Array(101).fill(0);
function fibonacci()
{
fib[1] = 1;
fib[2] = 2;
for (i = 3; i <= 87; i++)
{
fib[i] = fib[i - 1] + fib[i - 2];
}
}
function find(num)
{
var cnt = 0;
for (i = 87; i > 0; i--)
{
if (num >= fib[i])
{
v[cnt++] = i;
num -= fib[i];
}
}
for (i = 0; i < cnt / 2; i++)
{
var t = v[i];
v[i] = v[cnt - i - 1];
v[cnt - i - 1] = t;
}
dp1[0] = 1;
dp2[0] = parseInt((v[0] - 1) / 2);
for (i = 1; i < cnt; i++)
{
dp1[i] = dp1[i - 1] + dp2[i - 1];
dp2[i] = parseInt((v[i] - v[i - 1]) /
2) * dp2[i - 1] +
parseInt((v[i] - v[i - 1] - 1) /
2) * dp1[i - 1];
}
return (dp1[cnt - 1] + dp2[cnt - 1]);
}
fibonacci();
var num = 13;
document.write(find(num));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(log N)
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!
Last Updated :
18 Jan, 2022
Like Article
Save Article