通过将奇数大小的子数组移动到给定数组的末尾,最大化偶数索引和奇数索引处元素总和的差异。
给定一个大小为N的数组arr[] ,任务是通过将任何奇数长度的子数组移动到数组的末尾来最大化偶数索引处的元素和奇数索引处的元素之和的差异。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 3
Explanation: Initially sum of elements at even indices = 1 + 3 + 5 = 9
Sum of elements at odd indices = 2 + 4 + 6 = 12
Difference = 9 -12 = -3
On shifting the subarray from [0, 2] to the end the array becomes {4, 5, 6, 1, 2, 3}
Sum of elements at even indices = 4 + 6 + 2 = 12
Sum of elements at odd indices = 5 + 1 + 3 = 9
Difference = 12 – 9 = 3
This gives the maximum answer out of all such shifts that is equal to 3.
Input: arr[] = {3, 4, 8}
Output: 7
方法:可以使用前缀和技术以数学方式解决该任务。问题可以分为4种情况:
Case 1: When length of the array N is even and l (start of subarray) is even {a,b,c,d,e,f} for l = 2, r =4 (one based indexing)
Initial: +a – b + c – d + e -f on shifting sub array of [2,4] to the end the array becomes ={a, e, f, b, c, d}
Final: + a- e +f – b + c-d
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] remains same and part3 (r, N] becomes negative (opposite sign) after shifting.
If prefix_sum array is computed then sum after shifting becomes
sum1 = prefix_sum(l-1) + ( prefix_sum(r) – prefix_sum(l-1) ) – ((prefix_sum(n) – prefix_sum(r))
sum1 = 2* prefix_sum(r) – prefix_sum(N)
Case 2: When length of the array N is even and l (start of subarray) is odd {a, b, c, d, e, f} for l = 3, r = 5(one based indexing)
Initial: +a – b + c – d + e -f on shifting [3,5] to the end the array becomes = {a, b, f, c, d, e}
Final: + a – b + f – c + d – e
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] becomes opposite sign and part3 (r, N] becomes opposite sign after shifting.
If prefix_sum array is computed then sum after shifting becomes
sum2 = prefix_sum(l-1) – ( prefix_sum(r) – prefix_sum(l-1)) – ((prefix_sum(n) – prefix_sum(r))
sum2 = 2* prefix_sum(l-1) – prefix_sum(N)
Case 3: When length of the array N is odd and l (start of subarray) is even {a, b, c, d, e} for l = 2, r = 4 (one based indexing)
Initial: +a – b + c – d + e on shifting sub array of [2,4] to the end the array becomes = {a, e, b, c, d}
Final: + a – e + b – c + d
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] becomes opposite sign and part3 (r, N] becomes negative(opposite sign) after shifting.
If prefix_sum array is computed then sum after shifting becomes
sum3 = prefix_sum(l-1) – (prefix_sum(r) – prefix_sum(l-1)) – ((prefix_sum(n) – prefix_sum(r))
sum3 = 2* prefix_sum(l-1) – prefix_sum(N)
Case 4: When length of the array N is odd and l (start of subarray) is odd {a, b, c, d, e} for l = 3, r =3(one based indexing)
Initial: +a – b + c – d + e on shifting [3,5] to the end the array becomes = {a, b, d, e, c}
Final: + a – b + d – e + c
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] becomes opposite sign and part3 (r, N] remains same.
If prefix_sum array is computed then sum after shifting becomes
sum4 = prefix_sum(l-1) + (prefix_sum(r) – prefix_sum(l-1)) – ((prefix_sum(n) – prefix_sum(r))
sum4 = 2* prefix_sum(r) – prefix_sum(N)
- If n is even result becomes: max(sum1, sum2).
- If n is odd result becomes: max(sum3,sum4).
请按照以下步骤解决问题:
- 用 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)