📜  偶数长度的最大和子数组

📅  最后修改于: 2021-04-23 20:25:32             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是找到长度为X的任何子数组的最大和,使得X> 0X%2 = 0

例子:

方法:该问题是最大子数组和问题的一种变体,可以使用动态编程方法解决。创建一个数组dp [] ,其中dp [i]将存储第一个元素为arr [i]的偶数长度子数组的最大和。现在,递归关系将是:

这是因为以元素arr [i]开头的最大最大偶数长度子数组可以是arr [i]arr [i + 1]的和,也可以是arr [i] + arr [i + 1]的和arr [i + 2]dp [i + 2]开头的偶数长度子数组的最大和。取两者中的最大值。
最后,来自dp []数组的最大值将是必需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum
// subarray sum of even length
int maxEvenLenSum(int arr[], int n)
{
  
    // There has to be at
    // least 2 elements
    if (n < 2)
        return 0;
  
    // dp[i] will store the maximum
    // subarray sum of even length
    // starting at arr[i]
    int dp[n] = { 0 };
  
    // Valid subarray cannot start from
    // the last element as its
    // length has to be even
    dp[n - 1] = 0;
    dp[n - 2] = arr[n - 2] + arr[n - 1];
  
    for (int i = n - 3; i >= 0; i--) {
  
        // arr[i] and arr[i + 1] can be added
        // to get an even length subarray
        // starting at arr[i]
        dp[i] = arr[i] + arr[i + 1];
  
        // If the sum of the valid subarray starting
        // from arr[i + 2] is greater than 0 then it
        // can be added with arr[i] and arr[i + 1]
        // to maximize the sum of the subarray
        // starting from arr[i]
        if (dp[i + 2] > 0)
            dp[i] += dp[i + 2];
    }
  
    // Get the sum of the even length
    // subarray with maximum sum
    int maxSum = *max_element(dp, dp + n);
    return maxSum;
}
  
// Driver code
int main()
{
  
    int arr[] = { 8, 9, -8, 9, 10 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxEvenLenSum(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
  
class GFG 
{
  
// Function to return the maximum
// subarray sum of even length
static int maxEvenLenSum(int arr[], int n)
{
  
    // There has to be at
    // least 2 elements
    if (n < 2)
        return 0;
  
    // dp[i] will store the maximum
    // subarray sum of even length
    // starting at arr[i]
    int []dp = new int[n];
  
    // Valid subarray cannot start from
    // the last element as its
    // length has to be even
    dp[n - 1] = 0;
    dp[n - 2] = arr[n - 2] + arr[n - 1];
  
    for (int i = n - 3; i >= 0; i--) 
    {
  
        // arr[i] and arr[i + 1] can be added
        // to get an even length subarray
        // starting at arr[i]
        dp[i] = arr[i] + arr[i + 1];
  
        // If the sum of the valid subarray starting
        // from arr[i + 2] is greater than 0 then it
        // can be added with arr[i] and arr[i + 1]
        // to maximize the sum of the subarray
        // starting from arr[i]
        if (dp[i + 2] > 0)
            dp[i] += dp[i + 2];
    }
  
    // Get the sum of the even length
    // subarray with maximum sum
    int maxSum = Arrays.stream(dp).max().getAsInt();
    return maxSum;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 8, 9, -8, 9, 10 };
    int n = arr.length;
  
    System.out.println(maxEvenLenSum(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to return the maximum
# subarray sum of even length
def maxEvenLenSum(arr, n):
  
    # There has to be at
    # least 2 elements
    if (n < 2):
        return 0
  
    # dp[i] will store the maximum
    # subarray sum of even length
    # starting at arr[i]
    dp = [0 for i in range(n)]
  
    # Valid subarray cannot start from
    # the last element as its
    # length has to be even
    dp[n - 1] = 0
    dp[n - 2] = arr[n - 2] + arr[n - 1]
  
    for i in range(n - 3, -1, -1):
  
        # arr[i] and arr[i + 1] can be added
        # to get an even length subarray
        # starting at arr[i]
        dp[i] = arr[i] + arr[i + 1]
  
        # If the sum of the valid subarray 
        # starting from arr[i + 2] is 
        # greater than 0 then it can be added 
        # with arr[i] and arr[i + 1]
        # to maximize the sum of the 
        # subarray starting from arr[i]
        if (dp[i + 2] > 0):
            dp[i] += dp[i + 2]
  
    # Get the sum of the even length
    # subarray with maximum sum
    maxSum = max(dp)
    return maxSum
  
# Driver code
arr = [8, 9, -8, 9, 10]
n = len(arr)
  
print(maxEvenLenSum(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
    static int MaxSum(int []arr)
    { 
           
        // assigning first element to the array
        int large = arr[0];
          
        // loop to compare value of large
        // with other elements
        for (int i = 1; i < arr.Length; i++)
        {
            // if large is smaller than other element
            // assig that element to the large
            if (large < arr[i])
                large = arr[i];
        }
        return large;
    }
      
    // Function to return the maximum 
    // subarray sum of even length 
    static int maxEvenLenSum(int []arr, int n) 
    { 
      
        // There has to be at 
        // least 2 elements 
        if (n < 2) 
            return 0; 
      
        // dp[i] will store the maximum 
        // subarray sum of even length 
        // starting at arr[i] 
        int []dp = new int[n]; 
      
        // Valid subarray cannot start from 
        // the last element as its 
        // length has to be even 
        dp[n - 1] = 0; 
        dp[n - 2] = arr[n - 2] + arr[n - 1]; 
      
        for (int i = n - 3; i >= 0; i--) 
        { 
      
            // arr[i] and arr[i + 1] can be added 
            // to get an even length subarray 
            // starting at arr[i] 
            dp[i] = arr[i] + arr[i + 1]; 
      
            // If the sum of the valid subarray starting 
            // from arr[i + 2] is greater than 0 then it 
            // can be added with arr[i] and arr[i + 1] 
            // to maximize the sum of the subarray 
            // starting from arr[i] 
            if (dp[i + 2] > 0) 
                dp[i] += dp[i + 2]; 
        } 
      
        // Get the sum of the even length 
        // subarray with maximum sum 
        int maxSum = MaxSum(dp);
        return maxSum; 
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int []arr = { 8, 9, -8, 9, 10 }; 
        int n = arr.Length; 
      
        Console.WriteLine(maxEvenLenSum(arr, n)); 
    } 
}
  
// This code is contributed by kanugargng


输出:
20

时间复杂度: O(n)
空间复杂度: O(n)