📜  具有最大和的最长子序列

📅  最后修改于: 2021-09-02 06:33:11             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是从给定数组中找到最长的非空子序列,其总和为最大值。

例子:

朴素的方法:解决这个问题最简单的方法是遍历数组并生成给定数组的所有可能子序列并计算它们的和。打印具有最大和的所有子序列中最长的。

时间复杂度: O(N * 2 N )
辅助空间: O(N)

高效方法:该问题可以使用贪婪技术解决。请按照以下步骤解决问题:

  • 初始化一个变量,比如maxm ,以存储给定数组的最大元素。
  • 如果maxm < 0 ,则打印maxm的值。
  • 否则,遍历数组并打印所有正数组元素。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the longest subsequence
// from the given array with maximum sum
void longestSubWithMaxSum(int arr[], int N)
{
    // Stores the largest element
    // of the array
    int Max = *max_element(arr,
                           arr + N);
 
    // If Max is less than 0
    if (Max < 0) {
 
        // Print the largest element
        // of the array
        cout << Max;
        return;
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is greater
        // than or equal to 0
        if (arr[i] >= 0) {
 
            // Print elements of
            // the subsequence
            cout << arr[i] << " ";
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, -4, -2, 3, 0 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    longestSubWithMaxSum(arr, N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
  
// Function to find the longest subsequence
// from the given array with maximum sum
static void longestSubWithMaxSum(int arr[], int N)
{
     
    // Stores the largest element
    // of the array
    int Max = Arrays.stream(arr).max().getAsInt();
  
    // If Max is less than 0
    if (Max < 0)
    {
         
        // Print the largest element
        // of the array
        System.out.print(Max);
        return;
    }
  
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is greater
        // than or equal to 0
        if (arr[i] >= 0)
        {
             
            // Print elements of
            // the subsequence
            System.out.print(arr[i] + " ");
        }
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, -4, -2, 3, 0 };
    int N = arr.length;
  
    longestSubWithMaxSum(arr, N);
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
 
# Function to find the longest subsequence
# from the given array with maximum sum
def longestSubWithMaxSum(arr, N):
 
    # Stores the largest element
    # of the array
    Max = max(arr)
 
    # If Max is less than 0
    if (Max < 0) :
 
        # Print the largest element
        # of the array
        print(Max)
        return
 
    # Traverse the array
    for i in range(N):
 
        # If arr[i] is greater
        # than or equal to 0
        if (arr[i] >= 0) :
 
            # Print elements of
            # the subsequence
            print(arr[i], end = " ")
 
# Driver code
arr = [ 1, 2, -4, -2, 3, 0 ]
 
N = len(arr)
 
longestSubWithMaxSum(arr, N)
 
# This code is contributed divyeshrabadiya07


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
  
// Function to find the longest subsequence
// from the given array with maximum sum
static void longestSubWithMaxSum(int []arr,
                                 int N)
{
     
    // Stores the largest element
    // of the array
    int Max = arr[0];
     
    for(int i = 1; i < N; i++)
    {
        if (Max < arr[i])
            Max = arr[i];
    }
     
    // If Max is less than 0
    if (Max < 0)
    {
         
        // Print the largest element
        // of the array
        Console.Write(Max);
        return;
    }
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is greater
        // than or equal to 0
        if (arr[i] >= 0)
        {
             
            // Print elements of
            // the subsequence
            Console.Write(arr[i] + " ");
        }
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, -4, -2, 3, 0 };
    int N = arr.Length;
  
    longestSubWithMaxSum(arr, N);
}
}
 
// This code is contributed by aashish1995


Javascript


输出:
1 2 3 0

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live