Longest sub | Function to return the length of the largest subsequence with non - negative sum ; To store the current sum ; Sort the input array in non - increasing order ; Traverse through the array ; Add the current element to the sum ; Condition when c_sum falls below zero ; Complete array has a non - negative sum ; Driver code
function maxLen ( arr , n ) { var c_sum = 0 ; arr . sort ( ( a , b ) => b - a ) for ( var i = 0 ; i < n ; i ++ ) { c_sum += arr [ i ] ; if ( c_sum < 0 ) return i ; } return n ; } var arr = [ 3 , 5 , - 6 ] ; var n = arr . length ; document . write ( maxLen ( arr , n ) ) ;