Find two Fibonacci numbers whose sum can be represented as N | C ++ program to find two Fibonacci numbers whose sum can be represented as N ; Function to create hash table to check Fibonacci numbers ; Storing the first two numbers in the hash ; Finding Fibonacci numbers up to N and storing them in the hash ; Function to find the Fibonacci pair with the given sum ; creating a set containing all fibonacci numbers ; Traversing all numbers to find first pair ; If both i and ( N - i ) are Fibonacci ; Printing the pair because i + ( N - i ) = N ; If no fibonacci pair is found whose sum is equal to n ; Driven code
#include <bits/stdc++.h> NEW_LINE 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 ; } } void findFibonacciPair ( int n ) { set < int > hash ; createHash ( hash , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . find ( i ) != hash . end ( ) && hash . find ( n - i ) != hash . end ( ) ) { cout << i << " , ▁ " << ( n - i ) << endl ; return ; } } cout << " - 1 STRNEWLINE " ; } int main ( ) { int N = 90 ; findFibonacciPair ( N ) ; return 0 ; }