Check if sum of Fibonacci elements in an Array is a Fibonacci number or not | Javascript program to check whether the sum of fibonacci elements of the array is a Fibonacci number or not ; Hash to store the Fibonacci numbers up to Max ; Function to create the hash table to check Fibonacci numbers ; Inserting the first two Fibonacci numbers leto the hash ; Add the remaining Fibonacci numbers based on the previous two numbers ; Function to check if the sum of Fibonacci numbers is Fibonacci or not ; Find the sum of all Fibonacci numbers ; Iterating through the array ; If the sum is Fibonacci then return true ; array of elements ; Creating a set containing all fibonacci numbers
let MAX = 100005 ; let fibonacci = new Set ( ) ; function createHash ( ) { let prev = 0 , curr = 1 ; fibonacci . add ( prev ) ; fibonacci . add ( curr ) ; while ( curr <= MAX ) { let temp = curr + prev ; fibonacci . add ( temp ) ; prev = curr ; curr = temp ; } } function checkArray ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( fibonacci . has ( arr [ i ] ) ) sum += arr [ i ] ; if ( fibonacci . has ( sum ) ) return true ; return false ; } let arr = [ 1 , 2 , 4 , 8 , 2 ] ; let n = arr . length ; createHash ( ) ; if ( checkArray ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;