# Count of Fibonacci pairs with sum N in range 0 to N

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}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. The idea is to use hashing to precompute and store the Fibonacci numbers less than equal to N in a hash
2. Initialize a counter variable as 0
3. Then for each element K in that hash, check if N – K is also present in the hash.
4. If both K and N – K are in hash, increment the counter variable

Below is the implementation of the above approach:

## CPP

 `// C++ program to find count of ` `// Fibonacci pairs whose ` `// sum can be represented as N ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to create hash table ` `// to check Fibonacci numbers ` `void` `createHash(set<``int``>& hash, ``int` `maxElement) ` `{ ` `    ``// Storing the first two numbers ` `    ``// in the hash ` `    ``int` `prev = 0, curr = 1; ` `    ``hash.insert(prev); ` `    ``hash.insert(curr); ` ` `  `    ``// Finding Fibonacci numbers up to N ` `    ``// and storing them in the hash ` `    ``while` `(curr < maxElement) { ` ` `  `        ``int` `temp = curr + prev; ` `        ``hash.insert(temp); ` `        ``prev = curr; ` `        ``curr = temp; ` `    ``} ` `} ` ` `  `// Function to find count of Fibonacci ` `// pair with the given sum ` `int` `findFibonacciPairCount(``int` `N) ` `{ ` `    ``// creating a set containing ` `    ``// all fibonacci numbers ` `    ``set<``int``> hash; ` `    ``createHash(hash, N); ` ` `  `    ``// Initialize count with 0 ` `    ``int` `count = 0; ` ` `  `    ``// traverse the hash to find ` `    ``// pairs with sum as N ` `    ``set<``int``>::iterator itr; ` `    ``for` `(itr = hash.begin(); ` ` ``*itr <= (N / 2); ` ` ``itr++) { ` ` `  `        ``// If both *itr and  ` `//(N - *itr) are Fibonacci ` `        ``// increment the count ` `        ``if` `(hash.find(N - *itr) ` ` ``!= hash.end()) { ` ` `  `            ``// Increase the count ` `            ``count++; ` `        ``} ` `    ``} ` ` `  `    ``// Return the count ` `    ``return` `count; ` `} ` ` `  `// Driven code ` `int` `main() ` `{ ` `    ``int` `N = 90; ` `    ``cout << findFibonacciPairCount(N)  ` `<< endl; ` ` `  `    ``N = 3; ` `    ``cout << findFibonacciPairCount(N) ` ` ``<< endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find count of ` `// Fibonacci pairs whose ` `// sum can be represented as N ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Function to create hash table ` `// to check Fibonacci numbers ` `static` `void` `createHash(HashSet hash, ``int` `maxElement) ` `{ ` `    ``// Storing the first two numbers ` `    ``// in the hash ` `    ``int` `prev = ``0``, curr = ``1``; ` `    ``hash.add(prev); ` `    ``hash.add(curr); ` `  `  `    ``// Finding Fibonacci numbers up to N ` `    ``// and storing them in the hash ` `    ``while` `(curr < maxElement) { ` `  `  `        ``int` `temp = curr + prev; ` `        ``hash.add(temp); ` `        ``prev = curr; ` `        ``curr = temp; ` `    ``} ` `} ` `  `  `// Function to find count of Fibonacci ` `// pair with the given sum ` `static` `int` `findFibonacciPairCount(``int` `N) ` `{ ` `    ``// creating a set containing ` `    ``// all fibonacci numbers ` `    ``HashSet hash = ``new` `HashSet(); ` `    ``createHash(hash, N); ` `  `  `    ``// Initialize count with 0 ` `    ``int` `count = ``0``; ` `    ``int` `i = ``0``; ` ` `  `    ``// traverse the hash to find ` `    ``// pairs with sum as N ` `    ``for` `(``int` `itr : hash) { ` `        ``i++; ` `        `  `        ``// If both *itr and  ` `        ``//(N - *itr) are Fibonacci ` `        ``// increment the count ` `        ``if` `(hash.contains(N - itr)) { ` `  `  `            ``// Increase the count ` `            ``count++; ` `        ``} ` `        ``if``(i == hash.size()/``2``) ` `            ``break``; ` `    ``} ` `  `  `    ``// Return the count ` `    ``return` `count; ` `} ` `  `  `// Driven code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``90``; ` `    ``System.out.print(findFibonacciPairCount(N)  ` `+``"\n"``); ` `  `  `    ``N = ``3``; ` `    ``System.out.print(findFibonacciPairCount(N) ` ` ``+``"\n"``); ` `  `  `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## C#

 `// C# program to find count of ` `// Fibonacci pairs whose ` `// sum can be represented as N ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` `   `  `// Function to create hash table ` `// to check Fibonacci numbers ` `static` `void` `createHash(HashSet<``int``> hash, ``int` `maxElement) ` `{ ` `    ``// Storing the first two numbers ` `    ``// in the hash ` `    ``int` `prev = 0, curr = 1; ` `    ``hash.Add(prev); ` `    ``hash.Add(curr); ` `   `  `    ``// Finding Fibonacci numbers up to N ` `    ``// and storing them in the hash ` `    ``while` `(curr < maxElement) { ` `   `  `        ``int` `temp = curr + prev; ` `        ``hash.Add(temp); ` `        ``prev = curr; ` `        ``curr = temp; ` `    ``} ` `} ` `   `  `// Function to find count of Fibonacci ` `// pair with the given sum ` `static` `int` `findFibonacciPairCount(``int` `N) ` `{ ` `    ``// creating a set containing ` `    ``// all fibonacci numbers ` `    ``HashSet<``int``> hash = ``new` `HashSet<``int``>(); ` `    ``createHash(hash, N); ` `   `  `    ``// Initialize count with 0 ` `    ``int` `count = 0; ` `    ``int` `i = 0; ` `  `  `    ``// traverse the hash to find ` `    ``// pairs with sum as N ` `    ``foreach` `(``int` `itr ``in` `hash) { ` `        ``i++; ` `         `  `        ``// If both *itr and  ` `        ``//(N - *itr) are Fibonacci ` `        ``// increment the count ` `        ``if` `(hash.Contains(N - itr)) { ` `   `  `            ``// Increase the count ` `            ``count++; ` `        ``} ` `        ``if``(i == hash.Count/2) ` `            ``break``; ` `    ``} ` `   `  `    ``// Return the count ` `    ``return` `count; ` `} ` `   `  `// Driven code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `N = 90; ` `    ``Console.Write(findFibonacciPairCount(N)  ` `                    ``+``"\n"``); ` `   `  `    ``N = 3; ` `    ``Console.Write(findFibonacciPairCount(N) ` `                     ``+``"\n"``); ` `   `  `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```1
2
```

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)

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Improved By : princiraj1992

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.