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
using System ; class GFG { static int MaxNonEmpSubSeq ( int [ ] a , int n ) { int sum = 0 ; int max = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( max < a [ i ] ) { max = a [ i ] ; } } if ( max <= 0 ) { return max ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } static void Main ( ) { int [ ] arr = { - 2 , 11 , - 4 , 2 , - 3 , - 10 } ; int N = arr . Length ; Console . WriteLine ( MaxNonEmpSubSeq ( arr , N ) ) ; } }