📌  相关文章
📜  反转子数组以最大化给定数组的偶数索引元素的总和

📅  最后修改于: 2021-09-17 06:50:52             🧑  作者: Mango

给定一个数组arr[] ,任务是通过反转子数组来最大化偶数索引元素的总和并打印获得的最大总和。

例子:

天真的方法:
解决问题的最简单方法是通过将元素一个一个反转来生成所有可能的排列,并计算每个排列的偶数索引处的总和。打印所有排列中可能的最大和。

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

有效的方法:
通过使用动态规划来检查数组旋转的最大差异,可以将上述方法进一步优化为O(N) 计算复杂度
请按照以下步骤解决问题:

  • 将奇数索引处的元素与偶数索引处的元素进行比较并跟踪它们。
  • 初始化两个数组leftDP[]rightDP[]
  • 对于每个奇数索引, leftDP[]存储当前索引处的元素与其左侧元素的差值,rightDP[] 存储右侧元素的差值。
  • 如果为前一个指数计算的差值为正,则将其添加到当前差值中:
  • 否则,存储当前差异:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to return maximized sum
// at even indices
int maximizeSum(int arr[], int n)
{
    int sum = 0;
    for(int i = 0; i < n; i = i + 2)
        sum += arr[i];
 
    // Stores difference with
    // element on the left
    int leftDP[n / 2];
 
    // Stores difference with
    // element on the right
    int rightDP[n / 2];
 
    int c = 0;
 
    for(int i = 1; i < n; i = i + 2)
    {
         
        // Compute and store
        // left difference
        int leftDiff = arr[i] - arr[i - 1];
 
        // For first index
        if (c - 1 < 0)
            leftDP = leftDiff;
 
        else
        {
             
            // If previous difference
            // is positive
            if (leftDP > 0)
                leftDP = leftDiff + leftDP;
 
            // Otherwise
            else
                leftDP[i] = leftDiff;
        }
 
        int rightDiff;
 
        // For the last index
        if (i + 1 >= n)
            rightDiff = 0;
 
        // Otherwise
        else
            rightDiff = arr[i] - arr[i + 1];
 
        // For first index
        if (c - 1 < 0)
            rightDP = rightDiff;
        else
        {
             
            // If the previous difference
            // is positive
            if (rightDP > 0)
                rightDP = rightDiff +
                             rightDP;
            else
                rightDP = rightDiff;
        }
        c++;
    }
    int maxi = 0;
    for(int i = 0; i < n / 2; i++)
    {
        maxi = max(maxi, max(leftDP[i],
                            rightDP[i]));
    }
    return maxi + sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 7, 8, 4, 5, 7,
                  6, 8, 9, 7, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int ans = maximizeSum(arr, n);
     
    cout << (ans);
}
 
// This code is contributed by chitranayal


Java
// Java Program to implement
// the above approach
import java.io.*;
 
class GFG {
 
    // Function to return maximized sum
    // at even indices
    public static int maximizeSum(int[] arr)
    {
 
        int n = arr.length;
        int sum = 0;
        for (int i = 0; i < n; i = i + 2)
            sum += arr[i];
 
        // Stores difference with
        // element on the left
        int leftDP[] = new int[n / 2];
 
        // Stores difference with
        // element on the right
        int rightDP[] = new int[n / 2];
 
        int c = 0;
 
        for (int i = 1; i < n; i = i + 2) {
 
            // Compute and store
            // left difference
            int leftDiff = arr[i]
                           - arr[i - 1];
 
            // For first index
            if (c - 1 < 0)
                leftDP = leftDiff;
 
            else {
 
                // If previous difference
                // is positive
                if (leftDP > 0)
                    leftDP = leftDiff
                                + leftDP;
 
                // Otherwise
                else
                    leftDP[i] = leftDiff;
            }
 
            int rightDiff;
 
            // For the last index
            if (i + 1 >= arr.length)
                rightDiff = 0;
 
            // Otherwise
            else
                rightDiff = arr[i]
                            - arr[i + 1];
 
            // For first index
            if (c - 1 < 0)
                rightDP = rightDiff;
            else {
 
                // If the previous difference
                // is positive
                if (rightDP > 0)
                    rightDP = rightDiff
                                 + rightDP;
                else
                    rightDP = rightDiff;
            }
            c++;
        }
        int max = 0;
        for (int i = 0; i < n / 2; i++) {
            max = Math.max(max,
                           Math.max(
                               leftDP[i],
                               rightDP[i]));
        }
 
        return max + sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 7, 8, 4, 5, 7, 6,
                      8, 9, 7, 3 };
        int ans = maximizeSum(arr);
        System.out.println(ans);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to return maximized sum
# at even indices
def maximizeSum(arr):
 
    n = len(arr)
    sum = 0
 
    for i in range(0, n, 2):
        sum += arr[i]
 
    # Stores difference with
    # element on the left
    leftDP = [0] * (n)
 
    # Stores difference with
    # element on the right
    rightDP = [0] * (n)
 
    c = 0
    for i in range(1, n, 2):
 
        # Compute and store
        # left difference
        leftDiff = arr[i] - arr[i - 1]
 
        # For first index
        if (c - 1 < 0):
            leftDP[i] = leftDiff
        else:
 
            # If previous difference
            # is positive
            if (leftDP[i] > 0):
                leftDP[i] = (leftDiff +
                             leftDP[i - 1])
 
            # Otherwise
            else:
                leftDP[i] = leftDiff
 
        rightDiff = 0
 
        # For the last index
        if (i + 1 >= len(arr)):
            rightDiff = 0
             
        # Otherwise
        else:
            rightDiff = arr[i] - arr[i + 1]
 
        # For first index
        if (c - 1 < 0):
            rightDP[i] = rightDiff
        else:
 
            # If the previous difference
            # is positive
            if (rightDP[i] > 0):
                rightDP[i] = (rightDiff +
                              rightDP[i - 1])
            else:
                rightDP[i] = rightDiff
                 
        c += 1
 
    maxm = 0
 
    for i in range(n // 2):
        maxm = max(maxm, max(leftDP[i],
                            rightDP[i]))
 
    return maxm + sum
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 7, 8, 4, 5, 7,
            6, 8, 9, 7, 3 ]
    ans = maximizeSum(arr)
 
    print(ans)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to return maximized sum
// at even indices
public static int maximizeSum(int[] arr)
{
    int n = arr.Length;
    int sum = 0;
     
    for(int i = 0; i < n; i = i + 2)
        sum += arr[i];
 
    // Stores difference with
    // element on the left
    int []leftDP = new int[n / 2];
 
    // Stores difference with
    // element on the right
    int []rightDP = new int[n / 2];
 
    int c = 0;
 
    for(int i = 1; i < n; i = i + 2)
    {
         
        // Compute and store
        // left difference
        int leftDiff = arr[i] - arr[i - 1];
 
        // For first index
        if (c - 1 < 0)
            leftDP = leftDiff;
             
        else
        {
             
            // If previous difference
            // is positive
            if (leftDP > 0)
                leftDP = leftDiff +
                            leftDP;
                             
            // Otherwise
            else
                leftDP = leftDiff;
        }
 
        int rightDiff;
 
        // For the last index
        if (i + 1 >= arr.Length)
            rightDiff = 0;
 
        // Otherwise
        else
            rightDiff = arr[i] - arr[i + 1];
 
        // For first index
        if (c - 1 < 0)
            rightDP = rightDiff;
             
        else
        {
             
            // If the previous difference
            // is positive
            if (rightDP > 0)
                rightDP = rightDiff +
                             rightDP;
            else
                rightDP = rightDiff;
        }
        c++;
    }
     
    int max = 0;
     
    for(int i = 0; i < n / 2; i++)
    {
        max = Math.Max(max,
                       Math.Max(leftDP[i],
                               rightDP[i]));
    }
    return max + sum;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 7, 8, 4, 5, 7, 6,
                  8, 9, 7, 3 };
    int ans = maximizeSum(arr);
     
    Console.WriteLine(ans);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
37

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

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