Maximum Sum Subsequence | 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
function MaxNonEmpSubSeq ( a , n ) { let sum = 0 ; let max = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( max < a [ i ] ) { max = a [ i ] ; } } if ( max <= 0 ) { return max ; } for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } let arr = [ - 2 , 11 , - 4 , 2 , - 3 , - 10 ] ; let N = arr . length ; document . write ( MaxNonEmpSubSeq ( arr , N ) ) ;