📌  相关文章
📜  通过将奇数大小的子数组移动到给定数组的末尾,最大化偶数索引和奇数索引处元素总和的差异。

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

通过将奇数大小的子数组移动到给定数组的末尾,最大化偶数索引和奇数索引处元素总和的差异。

给定一个大小为N的数组arr[] ,任务是通过任何奇数长度的数组移动到数组的末尾最大化偶数索引处的元素和奇数索引处的元素之和的差异。

例子:  

方法:可以使用前缀和技术以数学方式解决该任务。问题可以分为4种情况:

请按照以下步骤解决问题:

  • 用 0 初始化大小为n+1的向量prefix_sum
  • 现在通过将它们乘以 -1 来修改数组的奇数索引。
  • 遍历 range[ 1,n]中的数组并填充prefix_sum向量。
  • 如果数组的大小是偶数
  • 将变量maxval初始化为-1e8以存储最大和。
  • 遍历前缀和向量并检查
    • 如果 i 甚至将 maxval 分配为max(maxval, 2 * prefix_sum[i] – prefix_sum[n])
    • 否则将 maxval 分配为max(maxval, 2 * prefix_sum[i] – prefix_sum[n])
  • 如果数组的大小是奇数
  • 将变量maxval初始化为-1e8以存储最大和。
  • 遍历前缀和向量并检查
    • 如果 i 甚至将 maxval 分配为max(maxval, 2 * prefix_sum[i – 1] – prefix_sum[n])
    • 否则将 maxval 分配为max(maxval, 2 * prefix_sum[i] – prefix_sum[n])

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum value of
// difference between sum of elements
// at even indices and sum of elements
// at odd indices by shifting
// a subarray of odd length to the
// end of the array
int find_maxsum(int arr[], int n)
{
    vector prefix_sum(n + 1, 0);
     
      // Modify the array to keep
    // alternative +ve and -ve
    for (int i = 0; i < n; i++) {
        if (i % 2 == 1) {
            arr[i] = -arr[i];
        }
    }
   
    // Function to find the prefix sum
    for (int i = 1; i <= n; i++) {
        prefix_sum[i] = prefix_sum[i - 1]
            + arr[i - 1];
    }
       
    // If n is even
    if (n % 2 == 0) {
         
          // Initialize the maxval to -1e8
        int maxval = -1e8;
        for (int i = 1; i <= n; i++) {
             
              // If i is even (case-1)
            if (i % 2 == 0) {
                maxval = max(maxval, 2 *
                             prefix_sum[i]
                             - prefix_sum[n]);
            }
           
            // If i is odd (case-2)
            else {
                maxval = max(maxval, 2 *
                             prefix_sum[i - 1]
                             - prefix_sum[n]);
            }
        }
       
        // return the maxval
        return maxval;
    }
    else {
        // Initialize the maxval to -1e8
        int maxval = -1e8;
        for (int i = 1; i <= n; i++) {
             
              // If i is even (case 3)
            if (i % 2 == 0) {
                maxval = max(maxval, 2 *
                             prefix_sum[i - 1]
                             - prefix_sum[n]);
            }
           
            // If i is odd (case 4)
            else {
                maxval = max(maxval, 2 *
                             prefix_sum[i]
                             - prefix_sum[n]);
            }
        }
       
        // Return the maxval
        return maxval;
    }
}
 
int main()
{
    // Initialize an array
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    cout << find_maxsum(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
 
  // Function to find the maximum value of
  // difference between sum of elements
  // at even indices and sum of elements
  // at odd indices by shifting
  // a subarray of odd length to the
  // end of the array
  static int find_maxsum(int arr[], int n)
  {
    int prefix_sum[] = new int[n + 1];
    for(int i = 0; i < n + 1; i++) {
      prefix_sum[i] = 0;
    }
 
    // Modify the array to keep
    // alternative +ve and -ve
    for (int i = 0; i < n; i++) {
      if (i % 2 == 1) {
        arr[i] = -arr[i];
      }
    }
 
    // Function to find the prefix sum
    for (int i = 1; i <= n; i++) {
      prefix_sum[i] = prefix_sum[i - 1]
        + arr[i - 1];
    }
 
    // If n is even
    if (n % 2 == 0) {
 
      // Initialize the maxval to -1e8
      int maxval = (int)-1e8;
      for (int i = 1; i <= n; i++) {
 
        // If i is even (case-1)
        if (i % 2 == 0) {
          maxval = Math.max(maxval, 2 *
                            prefix_sum[i]
                            - prefix_sum[n]);
        }
 
        // If i is odd (case-2)
        else {
          maxval = Math.max(maxval, 2 *
                            prefix_sum[i - 1]
                            - prefix_sum[n]);
        }
      }
 
      // return the maxval
      return maxval;
    }
    else
    {
       
      // Initialize the maxval to -1e8
      int maxval = (int)-1e8;
      for (int i = 1; i <= n; i++) {
 
        // If i is even (case 3)
        if (i % 2 == 0) {
          maxval = Math.max(maxval, 2 *
                            prefix_sum[i - 1]
                            - prefix_sum[n]);
        }
 
        // If i is odd (case 4)
        else {
          maxval = Math.max(maxval, 2 *
                            prefix_sum[i]
                            - prefix_sum[n]);
        }
      }
 
      // Return the maxval
      return maxval;
    }
  }
 
  public static void main(String args[])
  {
     
    // Initialize an array
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = arr.length;
 
    // Function call
    System.out.println(find_maxsum(arr, N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python program for the above approach
 
# Function to find the maximum value of
# difference between sum of elements
# at even indices and sum of elements
# at odd indices by shifting
# a subarray of odd length to the
# end of the array
def find_maxsum(arr, n):
    prefix_sum = [ 0 for i in range(n + 1)]
     
    # Modify the array to keep
    # alternative +ve and -ve
    for i in range(n):
        if (i % 2 == 1):
            arr[i] = -arr[i]
             
    # Function to find the prefix sum
    for i in range(1, n+1):
        prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1]
         
    # If n is even
    if (n % 2 == 0):
         
        # Initialize the maxval to -1e8
        maxval = -10**8
        for i in range(1,n+1):
            # If i is even (case-1)
            if (i % 2 == 0):
                maxval = max(maxval, 2 * prefix_sum[i] - prefix_sum[n])
                 
            # If i is odd (case-2)
            else:
                maxval = max(maxval, 2 * prefix_sum[i - 1]- prefix_sum[n])
                 
        # return the maxval
        return maxval
     
    else:
        # Initialize the maxval to -1e8
        maxval = -10**8
        for i in range(1, n+1):
             
            # If i is even (case 3)
            if (i % 2 == 0):
                maxval = max(maxval, 2 * prefix_sum[i - 1] - prefix_sum[n])
             
            # If i is odd (case 4)
            else:
                maxval = max(maxval, 2 * prefix_sum[i] - prefix_sum[n])
                 
        # Return the maxval
        return maxval
         
# Initialize an array
arr = [1, 2, 3, 4, 5, 6]
N = len(arr)
 
# Function call
print(find_maxsum(arr, N))
 
# This code is contributed by Shubham Singh


C#
// C# code to implement the above approach
using System;
public class GFG{
 
  // Function to find the maximum value of
  // difference between sum of elements
  // at even indices and sum of elements
  // at odd indices by shifting
  // a subarray of odd length to the
  // end of the array
  static int find_maxsum(int[] arr, int n)
  {
    int[] prefix_sum = new int[n + 1];
    for(int i = 0; i < n + 1; i++) {
      prefix_sum[i] = 0;
    }
 
    // Modify the array to keep
    // alternative +ve and -ve
    for (int i = 0; i < n; i++) {
      if (i % 2 == 1) {
        arr[i] = -arr[i];
      }
    }
 
    // Function to find the prefix sum
    for (int i = 1; i <= n; i++) {
      prefix_sum[i] = prefix_sum[i - 1]
        + arr[i - 1];
    }
 
    // If n is even
    if (n % 2 == 0) {
 
      // Initialize the maxval to -1e8
      int maxval = (int)-1e8;
      for (int i = 1; i <= n; i++) {
 
        // If i is even (case-1)
        if (i % 2 == 0) {
          maxval = Math.Max(maxval, 2 *
                            prefix_sum[i]
                            - prefix_sum[n]);
        }
 
        // If i is odd (case-2)
        else {
          maxval = Math.Max(maxval, 2 *
                            prefix_sum[i - 1]
                            - prefix_sum[n]);
        }
      }
 
      // return the maxval
      return maxval;
    }
    else
    {
 
      // Initialize the maxval to -1e8
      int maxval = (int)-1e8;
      for (int i = 1; i <= n; i++) {
 
        // If i is even (case 3)
        if (i % 2 == 0) {
          maxval = Math.Max(maxval, 2 *
                            prefix_sum[i - 1]
                            - prefix_sum[n]);
        }
 
        // If i is odd (case 4)
        else {
          maxval = Math.Max(maxval, 2 *
                            prefix_sum[i]
                            - prefix_sum[n]);
        }
      }
 
      // Return the maxval
      return maxval;
    }
  }
 
  // Driver code
  public static void Main()
  {
 
    // Initialize an array
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    int N = arr.Length;
 
    // Function call
    Console.Write(find_maxsum(arr, N));
  }
}
 
// This code is contributed by Shubham Singh


Javascript



输出
3

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