给定一个由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
方法:为了最大化最后剩余元素的值,有以下三种情况:
- 数组同时具有负数和正数:首先,我们将从负数中减去所有正数(一个除外)。此后,我们将只剩下一个正数和一个负数。现在,我们将从正数中减去该负数,最终将得到一个正数。因此,在这种情况下,结果是数组元素的绝对值之和。
- 数组仅包含正数:首先我们找到最小的数,然后从中减去除一个正数外的所有正数。此后,我们只得到一个正数和一个负数,现在我们将从该正数中减去负数,最后将得到一个正数。在这里我们可以看到最小的
number消失了,并且该值基本上也从与情况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
输出:
13
时间复杂度: O(N)