Pair of fibonacci numbers with a given sum and minimum absolute difference | C ++ program to find the pair of Fibonacci numbers with a given sum and minimum absolute difference ; Hash to store all the Fibonacci numbers ; Function to generate fibonacci Series and create hash table to check Fibonacci numbers ; Adding the first two Fibonacci numbers into the Hash set ; Computing the remaining Fibonacci numbers based on the previous two numbers ; Function to find the pair of Fibonacci numbers with the given sum and minimum absolute difference ; Start from N / 2 such that the difference between i and N - i will be minimum ; If both ' i ' and ' sum ▁ - ▁ i ' are fibonacci numbers then print them and break the loop ; If there is no Fibonacci pair possible ; Driver code ; Generate the Fibonacci numbers ; Find the Fibonacci pair
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000005 NEW_LINE set < int > fib ; void fibonacci ( ) { int prev = 0 , curr = 1 , len = 2 ; fib . insert ( prev ) ; fib . insert ( curr ) ; while ( len <= MAX ) { int temp = curr + prev ; fib . insert ( temp ) ; prev = curr ; curr = temp ; len ++ ; } } void findFibonacci ( int N ) { for ( int i = N / 2 ; i > 1 ; i -- ) { if ( fib . find ( i ) != fib . end ( ) && fib . find ( N - i ) != fib . end ( ) ) { cout << i << " ▁ " << ( N - i ) << endl ; return ; } } cout << " - 1" << endl ; } int main ( ) { fibonacci ( ) ; int sum = 199 ; findFibonacci ( sum ) ; return 0 ; }