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 leto 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 prlet them and break the loop ; If there is no Fibonacci pair possible ; Generate the Fibonacci numbers ; Find the Fibonacci pair
let MAX = 1000005 ; let fib = new Set ( ) ; function fibonacci ( ) { let prev = 0 , curr = 1 , len = 2 ; fib . add ( prev ) ; fib . add ( curr ) ; while ( len <= MAX ) { let temp = curr + prev ; fib . add ( temp ) ; prev = curr ; curr = temp ; len ++ ; } } function findFibonacci ( N ) { for ( let i = Math . floor ( N / 2 ) ; i > 1 ; i -- ) { if ( fib . has ( i ) && fib . has ( N - i ) ) { document . write ( i + " " + ( N - i ) ) ; return ; } } document . write ( " " ) ; } fibonacci ( ) ; let sum = 199 ; findFibonacci ( sum ) ;