📌  相关文章
📜  最大化子序列的偶数和奇数索引元素之和之间的差异 |设置 2

📅  最后修改于: 2022-05-13 01:56:04.402000             🧑  作者: Mango

最大化子序列的偶数和奇数索引元素之和之间的差异 |设置 2

给定一个由N个正整数组成的数组arr[] ,任务是为数组的任何子序列找到偶数索引和奇数索引处元素之和之间差异的最大值。

例子:

朴素方法:解决给定问题的简单方法是生成给定数组的所有可能子序列,并为每个子序列最大化偶数和奇数索引处元素之和之间的差异。检查所有子序列后,打印获得的最大值。

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

局部最大值方法:本文讨论了使用局部最大值和局部最小值解决此问题的方法。在本文中,我们讨论了动态规划方法。

高效方法:上述方法也可以通过使用动态规划进行优化,因为上述方法具有最优子结构和重叠子问题。请按照以下步骤解决给定的问题:

  • 初始化大小为N的两个数组dp1[]dp2[] ,并使用值-1进行初始化,以便dp1[i]存储从奇数长度的子序列到第i索引的最大和,并且dp2[i]存储偶数长度子序列的最大和 直到第i索引。
  • dp1[0]的值更新为arr[0]并将dp2[0]的值更新为0
  • 使用变量i迭代范围[1, N]并执行以下步骤:
    • dp1[i]的值更新为dp1[i] = max(dp1[i – 1], dp2[i – 1] + arr[i]) ,因为第i 个元素将附加在子序列中的偶数索引处.因此,添加了数组元素arr[i]
    • dp2[i]的值更新为dp2[i] = max(dp2[i – 1], dp1[i – 1] – arr[i])因为第i 个元素将附加在子序列中的奇数索引处。因此,数组元素arr[i]被减去。
  • 执行上述步骤后,打印(dp1[N – 1], dp2[N – 1])的最大值作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
void findMaximumPeakSum(int arr[], int n)
{
    // Initialize the two arrays
    int dp1[n], dp2[n];
    for (int i = 0; i < n; i++) {
        dp1[i] = -1;
        dp2[i] = -1;
    }
    dp2[0] = 0;
    dp1[0] = arr[0];
 
    // Iterate over the range
    for (int i = 1; i < n; i++) {
 
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        dp1[i] = max(dp1[i - 1],
                     dp2[i - 1] + arr[i]);
        dp2[i] = max(dp2[i - 1],
                     dp1[i - 1] - arr[i]);
    }
 
    // Find the maximum value
    int ans = max(dp1[n - 1], dp2[n - 1]);
 
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    findMaximumPeakSum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
static void findMaximumPeakSum(int arr[], int n)
{
   
    // Initialize the two arrays
    int []dp1 = new int[n];
    int []dp2 = new int[n];
    for (int i = 0; i < n; i++) {
        dp1[i] = -1;
        dp2[i] = -1;
    }
    dp2[0] = 0;
    dp1[0] = arr[0];
 
    // Iterate over the range
    for (int i = 1; i < n; i++) {
 
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        dp1[i] = Math.max(dp1[i - 1],
                     dp2[i - 1] + arr[i]);
        dp2[i] = Math.max(dp2[i - 1],
                     dp1[i - 1] - arr[i]);
    }
 
    // Find the maximum value
    int ans = Math.max(dp1[n - 1], dp2[n - 1]);
 
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = arr.length;
 
    findMaximumPeakSum(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python Program to implement
# the above approach
 
# Function to find the maximum value
# of difference between the sum of
# elements at even and odd indices of
# any subsequence of the array
def findMaximumPeakSum(arr, n):
   
    # Initialize the two arrays
    dp1 = [0] * n
    dp2 = [0] * n
    for i in range(n):
        dp1[i] = -1
        dp2[i] = -1
 
    dp2[0] = 0
    dp1[0] = arr[0]
 
    # Iterate over the range
    for i in range(1, n):
 
        # Find the maximum sum upto the
        # i-th odd and even subsequence
        dp1[i] = max(dp1[i - 1],
                     dp2[i - 1] + arr[i])
        dp2[i] = max(dp2[i - 1],
                     dp1[i - 1] - arr[i])
 
    # Find the maximum value
    ans = max(dp1[n - 1], dp2[n - 1])
 
    print(ans)
 
# Driver Code
arr = [3, 2, 1, 4, 5, 2, 1, 7, 8, 9]
N = len(arr)
 
findMaximumPeakSum(arr, N)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
 
public class GFG{
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
static void findMaximumPeakSum(int []arr, int n)
{
   
    // Initialize the two arrays
    int []dp1 = new int[n];
    int []dp2 = new int[n];
    for (int i = 0; i < n; i++) {
        dp1[i] = -1;
        dp2[i] = -1;
    }
    dp2[0] = 0;
    dp1[0] = arr[0];
 
    // Iterate over the range
    for (int i = 1; i < n; i++) {
 
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        dp1[i] = Math.Max(dp1[i - 1],
                     dp2[i - 1] + arr[i]);
        dp2[i] = Math.Max(dp2[i - 1],
                     dp1[i - 1] - arr[i]);
    }
 
    // Find the maximum value
    int ans = Math.Max(dp1[n - 1], dp2[n - 1]);
 
    Console.Write(ans);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = arr.Length;
 
    findMaximumPeakSum(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
void findMaximumPeakSum(int arr[], int n)
{
    // Initialize the variables
    int even = 0;
    int odd = arr[0];
 
    // Iterat over the range
    for (int i = 1; i < n; i++) {
 
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        int temp = odd;
        odd = max(odd, even + arr[i]);
        even = max(even, temp - arr[i]);
    }
 
    // Find the resultant maximum value
    int ans = max(odd, even);
 
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    findMaximumPeakSum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
static void findMaximumPeakSum(int arr[], int n)
{
   
    // Initialize the variables
    int even = 0;
    int odd = arr[0];
  
    // Iterat over the range
    for (int i = 1; i < n; i++) {
  
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        int temp = odd;
        odd = Math.max(odd, even + arr[i]);
        even = Math.max(even, temp - arr[i]);
    }
  
    // Find the resultant maximum value
    int ans = Math.max(odd, even);
  
    System.out.print(ans);
}
  
    // Driver Code
    public static void main(String[] args) {
        int arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = arr.length;
  
    findMaximumPeakSum(arr, N);
    }
}
 
// This code is contributed by code_hunt.


Python3
# Python 3 program for the above approach
 
# Function to find the maximum value
# of difference between the sum of
# elements at even and odd indices of
# any subsequence of the array
def findMaximumPeakSum(arr, n):
   
    # Initialize the variables
    even = 0
    odd = arr[0]
 
    # Iterat over the range
    for i in range(1,  n):
 
        # Find the maximum sum upto the
        # i-th odd and even subsequence
        temp = odd
        odd = max(odd, even + arr[i])
        even = max(even, temp - arr[i])
 
    # Find the resultant maximum value
    ans = max(odd, even)
 
    print(ans)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [3, 2, 1, 4, 5, 2, 1, 7, 8, 9]
    N = len(arr)
 
    findMaximumPeakSum(arr, N)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
class GFG {
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
static void findMaximumPeakSum(int[] arr, int n)
{
   
    // Initialize the variables
    int even = 0;
    int odd = arr[0];
  
    // Iterat over the range
    for (int i = 1; i < n; i++) {
  
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        int temp = odd;
        odd = Math.Max(odd, even + arr[i]);
        even = Math.Max(even, temp - arr[i]);
    }
  
    // Find the resultant maximum value
    int ans = Math.Max(odd, even);
  
    Console.Write(ans);
}
 
// Driver Code
public static void Main () {
         
    int[] arr = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = arr.Length;
  
    findMaximumPeakSum(arr, N);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
15

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

优化方法:可以通过使用两个变量来进一步优化上述方法,奇数偶数,而不是两个数组dp1[]dp2[] ,以保持偶数和奇数索引处元素总和之间的最大差异。对于每个索引i ,只需要前一个索引的偶数和奇数长度子序列的最大总和来计算当前状态。

下面是上述方法的实现:

C++14

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
void findMaximumPeakSum(int arr[], int n)
{
    // Initialize the variables
    int even = 0;
    int odd = arr[0];
 
    // Iterat over the range
    for (int i = 1; i < n; i++) {
 
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        int temp = odd;
        odd = max(odd, even + arr[i]);
        even = max(even, temp - arr[i]);
    }
 
    // Find the resultant maximum value
    int ans = max(odd, even);
 
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    findMaximumPeakSum(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG {
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
static void findMaximumPeakSum(int arr[], int n)
{
   
    // Initialize the variables
    int even = 0;
    int odd = arr[0];
  
    // Iterat over the range
    for (int i = 1; i < n; i++) {
  
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        int temp = odd;
        odd = Math.max(odd, even + arr[i]);
        even = Math.max(even, temp - arr[i]);
    }
  
    // Find the resultant maximum value
    int ans = Math.max(odd, even);
  
    System.out.print(ans);
}
  
    // Driver Code
    public static void main(String[] args) {
        int arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = arr.length;
  
    findMaximumPeakSum(arr, N);
    }
}
 
// This code is contributed by code_hunt.

Python3

# Python 3 program for the above approach
 
# Function to find the maximum value
# of difference between the sum of
# elements at even and odd indices of
# any subsequence of the array
def findMaximumPeakSum(arr, n):
   
    # Initialize the variables
    even = 0
    odd = arr[0]
 
    # Iterat over the range
    for i in range(1,  n):
 
        # Find the maximum sum upto the
        # i-th odd and even subsequence
        temp = odd
        odd = max(odd, even + arr[i])
        even = max(even, temp - arr[i])
 
    # Find the resultant maximum value
    ans = max(odd, even)
 
    print(ans)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [3, 2, 1, 4, 5, 2, 1, 7, 8, 9]
    N = len(arr)
 
    findMaximumPeakSum(arr, N)
 
    # This code is contributed by ukasp.

C#

// C# program for the above approach
using System;
class GFG {
 
// Function to find the maximum value
// of difference between the sum of
// elements at even and odd indices of
// any subsequence of the array
static void findMaximumPeakSum(int[] arr, int n)
{
   
    // Initialize the variables
    int even = 0;
    int odd = arr[0];
  
    // Iterat over the range
    for (int i = 1; i < n; i++) {
  
        // Find the maximum sum upto the
        // i-th odd and even subsequence
        int temp = odd;
        odd = Math.Max(odd, even + arr[i]);
        even = Math.Max(even, temp - arr[i]);
    }
  
    // Find the resultant maximum value
    int ans = Math.Max(odd, even);
  
    Console.Write(ans);
}
 
// Driver Code
public static void Main () {
         
    int[] arr = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 };
    int N = arr.Length;
  
    findMaximumPeakSum(arr, N);
}
}
 
// This code is contributed by sanjoy_62.

Javascript


输出
15

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