Count of Fibonacci pairs with sum N in range 0 to N
Last Updated :
07 Oct, 2021
Given a number N, the task is to find the count of Fibonacci pairs in range 0 to N whose sum is N.
Examples:
Input: N = 90
Output: 1
Explanation:
Only Fibonacci pair in range [0, 90] whose sum is equal to 90 is {1, 89}
Input: N = 3
Output: 2
Explanation:
Fibonacci Pair in range [0, 3] with whose sum is equal to 3 are {0, 3}, {1, 2}
Approach:
- The idea is to use hashing to precompute and store the Fibonacci numbers less than equal to N in a hash
- Initialize a counter variable as 0
- Then for each element K in that hash, check if N – K is also present in the hash.
- If both K and N – K are in hash, increment the counter variable
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void createHash(set< int >& hash, int maxElement)
{
int prev = 0, curr = 1;
hash.insert(prev);
hash.insert(curr);
while (curr < maxElement) {
int temp = curr + prev;
hash.insert(temp);
prev = curr;
curr = temp;
}
}
int findFibonacciPairCount( int N)
{
set< int > hash;
createHash(hash, N);
int count = 0;
set< int >::iterator itr;
for (itr = hash.begin();
*itr <= (N / 2);
itr++) {
if (hash.find(N - *itr)
!= hash.end()) {
count++;
}
}
return count;
}
int main()
{
int N = 90;
cout << findFibonacciPairCount(N)
<< endl;
N = 3;
cout << findFibonacciPairCount(N)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static void createHash(HashSet<Integer> hash, int maxElement)
{
int prev = 0 , curr = 1 ;
hash.add(prev);
hash.add(curr);
while (curr < maxElement) {
int temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
static int findFibonacciPairCount( int N)
{
HashSet<Integer> hash = new HashSet<Integer>();
createHash(hash, N);
int count = 0 ;
int i = 0 ;
for ( int itr : hash) {
i++;
if (hash.contains(N - itr)) {
count++;
}
if (i == hash.size()/ 2 )
break ;
}
return count;
}
public static void main(String[] args)
{
int N = 90 ;
System.out.print(findFibonacciPairCount(N)
+ "\n" );
N = 3 ;
System.out.print(findFibonacciPairCount(N)
+ "\n" );
}
}
|
Python3
def createHash( Hash , maxElement):
prev, curr = 0 , 1
Hash .add(prev)
Hash .add(curr)
while (curr < maxElement):
temp = curr + prev
Hash .add(temp)
prev = curr
curr = temp
def findFibonacciPairCount(N):
Hash = set ()
createHash( Hash , N)
count = 0
i = 0
for itr in Hash :
i + = 1
if ((N - itr) in Hash ):
count + = 1
if (i = = ( len ( Hash ) / / 2 )):
break
return count
N = 90
print (findFibonacciPairCount(N))
N = 3
print (findFibonacciPairCount(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void createHash(HashSet< int > hash, int maxElement)
{
int prev = 0, curr = 1;
hash.Add(prev);
hash.Add(curr);
while (curr < maxElement) {
int temp = curr + prev;
hash.Add(temp);
prev = curr;
curr = temp;
}
}
static int findFibonacciPairCount( int N)
{
HashSet< int > hash = new HashSet< int >();
createHash(hash, N);
int count = 0;
int i = 0;
foreach ( int itr in hash) {
i++;
if (hash.Contains(N - itr)) {
count++;
}
if (i == hash.Count/2)
break ;
}
return count;
}
public static void Main(String[] args)
{
int N = 90;
Console.Write(findFibonacciPairCount(N)
+ "\n" );
N = 3;
Console.Write(findFibonacciPairCount(N)
+ "\n" );
}
}
|
Javascript
<script>
function createHash(hash, maxElement)
{
var prev = 0, curr = 1;
hash.add(prev);
hash.add(curr);
while (curr < maxElement) {
var temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
function findFibonacciPairCount(N)
{
var hash = new Set();
createHash(hash, N);
var count = 0, i =0;
hash.forEach(element => {
i++;
if (hash.size >= parseInt(i*2))
{
if (hash.has(N-element))
{
count++;
}
}
});
return count;
}
var N = 90;
document.write( findFibonacciPairCount(N) + "<br>" )
N = 3;
document.write( findFibonacciPairCount(N) + "<br>" )
</script>
|
Performance Analysis:
- Time Complexity: In the above approach, building hashmap of fibonacci numbers less than N would be O(N) operation. Then, for each element in hashmap, we search for another element making the search time complexity to be O(N * log N). So overall time complexity is O(N * log N)
- Auxiliary Space Complexity: In the above approach, we are using extra space for storing hashmap values. So Auxiliary space complexity is O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...