Maximum Sum Subsequence | Java 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
import java . util . * ; 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 ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 11 , - 4 , 2 , - 3 , - 10 } ; int N = arr . length ; System . out . println ( MaxNonEmpSubSeq ( arr , N ) ) ; } }