📜  具有非负和的最长子序列

📅  最后修改于: 2021-10-26 06:45:28             🧑  作者: Mango

给定一个长度为N的数组arr[] ,任务是找到具有非负和的最大子序列的长度。
例子:

方法:想法是所有非负数都必须包含在子序列中,因为这些数字只会增加总和的值。
现在,在负数中不难看出,必须首先选择较大的。因此,只要它们不将总和的值降低到 0 以下,就继续以非递增顺序添加负数。这可以在对数组进行排序后完成。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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());
 
    // 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;
}


Java
// 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


Python3
# 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


C#
// 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


Javascript


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程