Longest subsequence having maximum sum | C ++ program to implement the above approach ; 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 elements of the subsequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestSubWithMaxSum ( int arr [ ] , int N ) { int Max = * max_element ( arr , arr + N ) ; if ( Max < 0 ) { cout << Max ; return ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 ) { cout << arr [ i ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 2 , -4 , -2 , 3 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; longestSubWithMaxSum ( arr , N ) ; return 0 ; }