使用给定操作最大化数组总和
有一个由 (2 * n – 1) 个整数组成的数组。我们可以更改数组中恰好 n 个元素的符号。换句话说,我们可以精确地选择 n 个数组元素,并将它们中的每一个乘以 -1。找到数组的最大和。
例子 :
Input : arr[] = 50 50 50
Output : 150
There is no need to change anything.
The sum of elements equals 150
which is maximum.
Input : arr[] = -1 -100 -1
Output : 100
Change the sign of the first two
elements. Sum of the elements
equal to 100.
方法:
第 1 步:-将循环迭代 2*n-1 次并重复第 2、3 和 4 步。
第2步: -计算没有。负数 (neg)。
第 3 步:-通过取数字的绝对值来计算数组的总和(总和)。
第 4 步:-通过取数字的绝对值 (min) 找到数组的最小数字。
第5步: -检查是否没有。负数是奇数,n 的值(给定)是偶数,然后从总和中减去 m 的两倍,这将是数组的 max_sum,否则 sum 的值将是数组的 max_sum。
下面是上述方法的实现:
C++
// CPP program to get the maximum
// sum of the array.
#include
using namespace std;
// function to find maximum sum
int maxSum(int arr[], int n)
{
int neg = 0, sum = 0,
m = INT_MAX, max_sum;
// step 1
for (int i = 0; i < 2 * n - 1; i++) {
// step 2
neg += (arr[i] < 0);
// step 3
sum += abs(arr[i]);
// step 4
m = min(m, abs(arr[i]));
}
// step 5
if (neg % 2 && n % 2 == 0) {
max_sum = sum -= 2 * m;
return (max_sum);
}
max_sum = sum;
return (max_sum);
}
// Driver Function
int main()
{
int arr[] = { -1, -100, -1 };
int n = 2;
cout << maxSum(arr, n) << endl;
return 0;
}
Java
// Java program to get the maximum
// sum of the array.
import java.io.*;
import java.math.*;
class GFG {
// function to find maximum sum
static int maxSum(int arr[], int n) {
int neg = 0, sum = 0, m = 100000000, max_sum;
// step 1
for (int i = 0; i < 2 * n - 1; i++) {
// step 2
if (arr[i] < 0)
neg += 1;
// step 3
sum += Math.abs(arr[i]);
// step 4
m = Math.min(m, Math.abs(arr[i]));
}
// step 5
if (neg % 2 == 1 && n % 2 == 0) {
max_sum = sum -= 2 * m;
return (max_sum);
}
max_sum = sum;
return (max_sum);
}
// Driver Function
public static void main(String args[]) {
int arr[] = {-1, -100, -1};
int n = 2;
System.out.println(maxSum(arr, n));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python3
# Python3 code to get the maximum
# sum of the array.
import sys
# function to find maximum sum
def maxSum (arr, n):
neg = 0
sum = 0
m = sys.maxsize
# step 1
for i in range(2 * n - 1):
# step 2
neg += (arr[i] < 0)
# step 3
sum += abs(arr[i])
# step 4
m = min(m, abs(arr[i]))
# step 5
if neg % 2 and n % 2 == 0:
max_sum = sum - 2 * m
return (max_sum)
max_sum = sum
return max_sum
# Driver Code
arr = [ -1, -100, -1 ]
n = 2
print( maxSum(arr, n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to get the maximum
// sum of the array.
using System;
class GFG
{
// function to find maximum sum
static int maxSum(int []arr, int n)
{
int neg = 0, sum = 0;
int m = 100000000, max_sum;
// step 1
for (int i = 0; i < 2 * n - 1; i++)
{
// step 2
if (arr[i] < 0)
neg += 1;
// step 3
sum += Math.Abs(arr[i]);
// step 4
m = Math.Min(m, Math.Abs(arr[i]));
}
// step 5
if (neg % 2 == 1 && n % 2 == 0)
{
max_sum = sum -= 2 * m;
return (max_sum);
}
max_sum = sum;
return (max_sum);
}
// Driver Code
public static void Main()
{
int []arr = {-1, -100, -1};
int n = 2;
Console.WriteLine(maxSum(arr, n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
100