📜  最大总子序列

📅  最后修改于: 2021-05-04 07:17:24             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是查找给定数组中存在的最大和非空子序列。

例子:

天真的方法:解决此问题的最简单方法是生成数组的所有可能的非空子序列,并计算该数组的每个子序列的总和。最后,打印从子序列获得的最大和。

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

高效方法:想法是遍历数组,计算数组的正元素之和,然后打印获得的和。请按照以下步骤解决问题:

  • 检查数组的最大元素是否大于0 。如果发现为真,则遍历数组并打印数组中所有正元素的总和。
  • 否则,打印数组中存在的最大元素。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to print the maximum
// non-emepty subsequence sum
int MaxNonEmpSubSeq(int a[], int n)
{
    // Stores the maximum non-emepty
    // subsequence sum in an array
    int sum = 0;
 
    // Stores the largest element
    // in the array
    int max = *max_element(a, a + n);
 
    if (max <= 0) {
 
        return max;
    }
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If a[i] is greater than 0
        if (a[i] > 0) {
 
            // Update sum
            sum += a[i];
        }
    }
    return sum;
}
 
// Driver Code
int main()
{
    int arr[] = { -2, 11, -4, 2, -3, -10 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << MaxNonEmpSubSeq(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to print the maximum
  // non-emepty subsequence sum
  static int MaxNonEmpSubSeq(int a[], int n)
  {
 
    // Stores the maximum non-emepty
    // subsequence sum in an array
    int sum = 0;
 
    // Stores the largest element
    // in the array
    int max = a[0];
    for(int i = 1; i < n; i++)
    {
      if(max < a[i])
      {
        max = a[i];
      }
    }
 
    if (max <= 0)
    {    
      return max;
    }
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // If a[i] is greater than 0
      if (a[i] > 0)
      {
 
        // Update sum
        sum += a[i];
      }
    }
    return sum;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int arr[] = { -2, 11, -4, 2, -3, -10 };
    int N = arr.length;
 
    System.out.println(MaxNonEmpSubSeq(arr, N));
  }
}
 
// This code is contributed by divyesh072019


Python3
# Python3 program to implement
# the above approach
 
# Function to prthe maxmimum
# non-emepty subsequence sum
def MaxNonEmpSubSeq(a, n):
     
    # Stores the maxmimum non-emepty
    # subsequence sum in an array
    sum = 0
 
    # Stores the largest element
    # in the array
    maxm = max(a)
 
    if (maxm <= 0):
        return maxm
 
    # Traverse the array
    for i in range(n):
         
        # If a[i] is greater than 0
        if (a[i] > 0):
             
            # Update sum
            sum += a[i]
             
    return sum
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ -2, 11, -4, 2, -3, -10 ]
    N = len(arr)
 
    print(MaxNonEmpSubSeq(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to print the maximum
// non-emepty subsequence sum
static int MaxNonEmpSubSeq(int[] a, int n)
{
     
    // Stores the maximum non-emepty
    // subsequence sum in an array
    int sum = 0;
     
    // Stores the largest element
    // in the array
    int max = a[0];
    for(int i = 1; i < n; i++)
    {
        if (max < a[i])
        {
            max = a[i];
        }
    }
     
    if (max <= 0)
    {
        return max;
    }
     
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If a[i] is greater than 0
        if (a[i] > 0)
        {
             
            // Update sum
            sum += a[i];
        }
    }
    return sum;
}
 
// Driver Code
static void Main()
{
    int[] arr = { -2, 11, -4, 2, -3, -10 };
    int N = arr.Length;
     
    Console.WriteLine(MaxNonEmpSubSeq(arr, N));
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
13

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