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:
CPP
// C++ program to find count of // Fibonacci pairs whose // sum can be represented as N #include <bits/stdc++.h> 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<Integer> 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<Integer> hash = new HashSet<Integer>(); 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 |
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.
Recommended Posts:
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Check if a M-th fibonacci number divides N-th fibonacci number
- Count of consecutive Fibonacci pairs in the given Array
- Count of Fibonacci pairs which satisfy the given equation
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Array range queries to count the number of Fibonacci numbers with updates
- Sum of Fibonacci Numbers in a range
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Last digit of sum of numbers in the given range in the Fibonacci series
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Array range queries to find the maximum Fibonacci number with updates
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Count the nodes whose sum with X is a Fibonacci number
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
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

