Maximum Sum Subsequence | C ++ program to implement the above approach ; Function to print the maximum non - emepty subsequence sum ; Stores the maximum non - emepty subsequence sum in an array ; Stores the largest element in the array ; Traverse the array ; If a [ i ] is greater than 0 ; Update sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxNonEmpSubSeq ( int a [ ] , int n ) { int sum = 0 ; int max = * max_element ( a , a + n ) ; if ( max <= 0 ) { return max ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } int main ( ) { int arr [ ] = { -2 , 11 , -4 , 2 , -3 , -10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MaxNonEmpSubSeq ( arr , N ) ; return 0 ; }