给定一个包含N 个元素的数组arr[] ,您必须对给定的数组执行以下操作,直到该数组减少为单个元素,
- 选择两个索引i和j使得i != j 。
- 将arr[i]替换为arr[i] – arr[j]并从数组中移除arr[j] 。
任务是最大化并打印数组的最后一个剩余元素的值。
例子:
Input: arr[] = {20, 3, -15, 7}
Output: 45
Step 1: We can remove 7 and replace -15 with -22.
step 2: We can remove 3 and replace -22 with -25.
step 3: We can remove -25 and replace 20 with 45.
So 45 is the maximum value that we can get.
Input: arr[] = {5, 4, 6, 2}
Output: 13
处理方式:为了使最后剩余元素的值最大化,分三种情况:
- 数组有负数和正数:首先我们将从负数中减去所有正数(除了一个)。在此之后,我们将只剩下一个正数和一个负数。现在,我们将从正数中减去该负数,结果最终会产生一个正数。因此,在这种情况下,结果是数组元素的绝对值之和。
- 数组只包含正数:首先我们找到最小的数,然后从中减去除一个正数之外的所有正数。在此之后,我们只得到一个正数和一个负数,现在我们将从正数中减去负数,最终得到一个正数。在这里我们可以观察到最小的
数字已经消失,并且该值基本上是从与情况 1 不同的下一个更大的元素中删除的。因此,在这种情况下,结果是数组元素的绝对值之和 – 2 * 最小元素。 - 数组只包含负数:首先我们找到最大的数,然后从中减去除一个负数之外的所有负数。在此之后,我们只得到一个负数和一个正数,现在我们将从正数中减去负数,最终得到一个正数。在这里我们可以观察到最大的数字已经消失了,而且这个值基本上是从下一个更大的元素中切出的,这与情况 1 不同。所以在这种情况下,结果是数组元素的绝对值之和 – 2 * 绝对值最大的元素。这里我们取最大值作为最大的绝对值在负数的情况下是最小的。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximized value
int find_maximum_value(int a[], int n)
{
int sum = 0;
int minimum = INT_MAX;
int pos = 0, neg = 0;
for (int i = 0; i < n; i++) {
// Overall minimum absolute value
// of some element from the array
minimum = min(minimum, abs(a[i]));
// Add all absolute values
sum += abs(a[i]);
// Count positive and negative elements
if (a[i] >= 0)
pos += 1;
else
neg += 1;
}
// Both positive and negative
// values are present
if (pos > 0 && neg > 0)
return sum;
// Only positive or negative
// values are present
return (sum - 2 * minimum);
}
// Driver code
int main()
{
int a[] = { 5, 4, 6, 2 };
int n = sizeof(a) / sizeof(a[0]);
cout << find_maximum_value(a, n);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG
{
// Function to return the maximized value
static int find_maximum_value(int a[], int n)
{
int sum = 0;
int minimum = Integer.MAX_VALUE;
int pos = 0, neg = 0;
for (int i = 0; i < n; i++)
{
// Overall minimum absolute value
// of some element from the array
minimum = Math.min(minimum, Math.abs(a[i]));
// Add all absolute values
sum += Math.abs(a[i]);
// Count positive and negative elements
if (a[i] >= 0)
pos += 1;
else
neg += 1;
}
// Both positive and negative
// values are present
if (pos > 0 && neg > 0)
return sum;
// Only positive or negative
// values are present
return (sum - 2 * minimum);
}
// Driver code
public static void main (String[] args)
{
int []a = { 5, 4, 6, 2 };
int n = a.length;
System.out.println(find_maximum_value(a, n));
}
}
// This code is contributed by ajit
Python
# Python3 implementation of the approach
# Function to return the maximized value
def find_maximum_value(a, n):
sum = 0
minimum = 10**9
pos = 0
neg = 0
for i in range(n):
# Overall minimum absolute value
# of some element from the array
minimum = min(minimum, abs(a[i]))
# Add all absolute values
sum += abs(a[i])
# Count positive and negative elements
if (a[i] >= 0):
pos += 1
else:
neg += 1
# Both positive and negative
# values are present
if (pos > 0 and neg > 0):
return sum
# Only positive or negative
# values are present
return (sum - 2 * minimum)
# Driver code
a= [5, 4, 6, 2]
n = len(a)
print(find_maximum_value(a, n))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximized value
static int find_maximum_value(int []a, int n)
{
int sum = 0;
int minimum = int.MaxValue;
int pos = 0, neg = 0;
for (int i = 0; i < n; i++)
{
// Overall minimum absolute value
// of some element from the array
minimum = Math.Min(minimum, Math.Abs(a[i]));
// Add all absolute values
sum += Math.Abs(a[i]);
// Count positive and negative elements
if (a[i] >= 0)
pos += 1;
else
neg += 1;
}
// Both positive and negative
// values are present
if (pos > 0 && neg > 0)
return sum;
// Only positive or negative
// values are present
return (sum - 2 * minimum);
}
// Driver code
static public void Main ()
{
int []a = { 5, 4, 6, 2 };
int n = a.Length;
Console.WriteLine(find_maximum_value(a, n));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
13
时间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。