Find the largest element in an array generated using the given conditions | C ++ program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findArray ( int n ) { vector < int > Arr ( n + 1 ) ; Arr [ 0 ] = 0 ; Arr [ 1 ] = 1 ; for ( int 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 ; } int maxElement ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; vector < int > Arr = findArray ( n ) ; return * max_element ( Arr . begin ( ) , Arr . end ( ) ) ; } int main ( ) { int N = 7 ; cout << maxElement ( N ) ; return 0 ; }