Longest sub-sequence with non-negative sum

Given an array arr[] of length N, the task is to find the length of the largest sub-sequence with non-negative sum.

Examples:

Input: arr[] = {1, 2, -3}
Output: 3
The complete array has a non-negative sum.



Input: arr[] = {1, 2, -4}
Output: 2
{1, 2} is the required subsequence.

Approach: The idea is that all the non-negative numbers must be included in the sub-sequence because such numbers will only increase the value of the total sum.
Now, it’s not hard to see among negative numbers, the larger ones must be chosen first. So, keep adding the negative numbers in non-increasing order of there values as long as they don’t decrease the value of the total sum below 0. This can be done after sorting the array.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the length of
// the largest subsequence
// with non-negative sum
int maxLen(int* arr, int n)
{
    // To store the current sum
    int c_sum = 0;
  
    // Sort the input array in
    // non-increasing order
    sort(arr, arr + n, greater<int>());
  
    // Traverse through the array
    for (int i = 0; i < n; i++) {
  
        // Add the current element to the sum
        c_sum += arr[i];
  
        // Condition when c_sum falls
        // below zero
        if (c_sum < 0)
            return i;
    }
  
    // Complete array has a non-negative sum
    return n;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 5, -6 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxLen(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to return the length of
// the largest subsequence
// with non-negative sum
static int maxLen(int[] arr, int n)
{
    // To store the current sum
    int c_sum = 0;
  
    // Sort the input array in
    // non-increasing order
    Arrays.sort(arr);
  
    // Traverse through the array
    for (int i = n-1; i >=0; i--)
    {
  
        // Add the current element to the sum
        c_sum += arr[i];
  
        // Condition when c_sum falls
        // below zero
        if (c_sum < 0)
            return i;
    }
  
    // Complete array has a non-negative sum
    return n;
}
  
// Driver code
public static void main(String []args)
{
    int arr[] = { 3, 5, -6 };
    int n = arr.length;
  
    System.out.println(maxLen(arr, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the length of 
# the largest subsequence 
# with non-negative sum 
def maxLen(arr, n) : 
  
    # To store the current sum 
    c_sum = 0
  
    # Sort the input array in 
    # non-increasing order 
    arr.sort(reverse = True); 
  
    # Traverse through the array 
    for i in range(n) :
  
        # Add the current element to the sum 
        c_sum += arr[i]; 
  
        # Condition when c_sum falls 
        # below zero 
        if (c_sum < 0) :
            return i; 
  
    # Complete array has a non-negative sum 
    return n; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 3, 5, -6 ]; 
    n = len(arr); 
  
    print(maxLen(arr, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System; 
  
class GFG 
{
  
// Function to return the length of
// the largest subsequence
// with non-negative sum
static int maxLen(int[] arr, int n)
{
    // To store the current sum
    int c_sum = 0;
  
    // Sort the input array in
    // non-increasing order
    Array.Sort(arr);
  
    // Traverse through the array
    for (int i = n - 1; i >= 0; i--)
    {
  
        // Add the current element to the sum
        c_sum += arr[i];
  
        // Condition when c_sum falls
        // below zero
        if (c_sum < 0)
            return i;
    }
  
    // Complete array has a non-negative sum
    return n;
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 3, 5, -6 };
    int n = arr.Length;
  
    Console.WriteLine(maxLen(arr, n));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.