Find the largest element in an array generated using the given conditions | Function to generate the required array and to find and return the maximum array element ; Stores the array ; Base case ; Iterate over the indices ; If current index is even ; Otherwise ; Function to find and return the maximum array element ; If n is 0 ; If n is 1 ; Generates the required array ; Return maximum element of Arr ; Driver Code
function findArray ( n ) { let Arr = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; Arr [ 0 ] = 0 ; Arr [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { Arr [ i ] = Arr [ i / 2 ] ; } else { Arr [ i ] = Arr [ ( i - 1 ) / 2 ] + Arr [ ( i - 1 ) / 2 + 1 ] ; } } return Arr ; } function maxElement ( n ) { if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; let Arr = findArray ( n ) ; let ans = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , Arr [ i ] ) ; } return ans ; } let N = 7 ; document . write ( maxElement ( N ) ) ;