Longest subsequence having maximum sum | Function to find the longest subsequence from the given array with maximum sum ; Stores the largest element of the array ; If Max is less than 0 ; Print the largest element of the array ; Traverse the array ; If arr [ i ] is greater than or equal to 0 ; Print the elements of the subsequence ; Driver code
function longestSubWithMaxSum ( arr , N ) { let Max = Math . max ( ... arr ) ; if ( Max < 0 ) { document . write ( Max ) ; return ; } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 ) { document . write ( arr [ i ] + " " ) ; } } } let arr = [ 1 , 2 , - 4 , - 2 , 3 , 0 ] ; let N = arr . length ; longestSubWithMaxSum ( arr , N ) ;