Length of longest Fibonacci subarray | A utility function that returns true if x is perfect square ; Returns true if n is a Fibinacci Number , else false ; Here n is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perferct square ; Function to find the length of the largest sub - array of an array every element of whose is a Fibonacci number ; Traverse the array arr [ ] ; Check if arr [ i ] is a Fibonacci number ; Stores the maximum length of the Fibonacci number subarray ; Finally , return the maximum length ; Given Input ; Function Call
function isPerfectSquare ( x ) { let s = parseInt ( Math . sqrt ( x ) ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } function contiguousFibonacciNumber ( arr , n ) { let current_length = 0 ; let max_length = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isFibonacci ( arr [ i ] ) ) { current_length ++ ; } else { current_length = 0 ; } max_length = Math . max ( max_length , current_length ) ; } return max_length ; } let arr = [ 11 , 8 , 21 , 5 , 3 , 28 , 4 ] ; let n = arr . length ; document . write ( contiguousFibonacciNumber ( arr , n ) ) ;