Maximum sum obtained by dividing Array into several subarrays as per given conditions | C ++ program for the above approach ; Function to find the required answer ; Stores maximum sum ; Adding the difference of elements at ends of increasing subarray to the answer ; Driver Code ; Input ; Functio calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( int arr [ ] , int N ) { int Sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) Sum += ( arr [ i ] - arr [ i - 1 ] ) ; } return Sum ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 } ; int N = ( sizeof ( arr ) / ( sizeof ( arr [ 0 ] ) ) ) ; cout << maximumSum ( arr , N ) ; return 0 ; }