Find the largest element in an array generated using the given conditions | Function to generate the required array ; 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
def findArray ( n ) : NEW_LINE INDENT Arr = [ 0 ] * ( n + 1 ) NEW_LINE Arr [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT Arr [ i ] = Arr [ i // 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT Arr [ i ] = ( Arr [ ( i - 1 ) // 2 ] + Arr [ ( i - 1 ) // 2 + 1 ] ) NEW_LINE DEDENT DEDENT return Arr NEW_LINE DEDENT def maxElement ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT Arr = findArray ( n ) NEW_LINE return max ( Arr ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE print ( maxElement ( N ) ) NEW_LINE DEDENT