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
def maxLen ( arr , n ) : NEW_LINE INDENT c_sum = 0 ; NEW_LINE arr . sort ( reverse = True ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c_sum += arr [ i ] ; NEW_LINE if ( c_sum < 0 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return n ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , - 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxLen ( arr , n ) ) ; NEW_LINE DEDENT