Given an array arr[] of size N, the task is to maximize the array sum by subtracting the absolute values of all odd and adding and absolute values of all even elements, with only at most one exceptional even-odd pair, i.e., only one even value can be subtracted and one odd element can be added.
Examples:
Input: arr[] = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}
Output: 4
Explanation: Total absolute value subtracted = 5 + 3 + 1 + 1 + 3 + 5 = 18.
Total absolute value added = 4 + 2 + 0 + 2 + 4 = 12
Sum achieved = -6. Make {5, 0} the exceptional elements
Add 5 to sum and subtract 0 from sum.
So total value subtracted = 13 and total value added = 17
New Sum = 17 – 3 = 4. This is the maximum possible sum.
Input: arr[] = {1, -2, 3}
Output: 0
Approach: The problem can be solved based on the following idea:
To maximize the sum, the maximum absolute value which is subtracted (say max) and the minimum absolute value which is added (say min) should be the exceptional elements. So the sum will increase by 2*(max – min).
Now, this should be performed only when min is less than max. Otherwise, the above term will become negative and will reduce the total sum.
Follow the below steps to solve the problem:
- Initialize the variables Max, Min, Sum to store maximum absolute odd, minimum absolute even and total sum respectively.
- Traverse the array from 0 to N – 1.
- If element is odd then subtract its absolute value from the Sum and update Max.
- If element is even then add its absolute value to the Sum and update Min.
- If Min is greater than Max then no need to update sum.
- Else, update the Sum, Sum = Sum + 2*(Max – Min).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int * arr, int N)
{
int Max = INT_MIN;
int Min = INT_MAX;
int X, Sum = 0;
for ( int i = 0; i < N; i++) {
X = arr[i];
if (X & 1) {
Max = max(Max, abs (X));
Sum -= abs (X);
}
else {
Min = min(Min, abs (X));
Sum += abs (X);
}
}
if (Min >= Max)
return Sum;
return (Sum + 2 * (Max - Min));
}
int main()
{
int arr[] = { -5, -4, -3, -2, -1,
0, 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxSum(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int maxSum( int arr[], int N)
{
int Max = Integer.MIN_VALUE;
int Min = Integer.MAX_VALUE;
int X, Sum = 0 ;
for ( int i = 0 ; i < N; i++) {
X = arr[i];
if ((X & 1 ) != 0 ) {
Max = Math.max(Max, Math.abs(X));
Sum -= Math.abs(X);
}
else {
Min = Math.min(Min, Math.abs(X));
Sum += Math.abs(X);
}
}
if (Min >= Max)
return Sum;
return (Sum + ( 2 * (Max - Min)));
}
public static void main(String[] args)
{
int arr[]
= { - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.print(maxSum(arr, N));
}
}
|
Python3
import sys
def maxSum(arr, N):
Max = - sys.maxsize - 1
Min = sys.maxsize
X, Sum = 0 , 0
for i in range (N):
X = arr[i]
if (X & 1 ):
Max = max ( Max , abs (X))
Sum - = abs (X)
else :
Min = min ( Min , abs (X))
Sum + = abs (X)
if ( Min > = Max ):
return Sum
return ( Sum + 2 * ( Max - Min ))
arr = [ - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
print (maxSum(arr, N))
|
C#
using System;
class GFG {
static int maxSum( int [] arr, int N)
{
int Max = Int32.MinValue;
int Min = Int32.MaxValue;
int X, Sum = 0;
for ( int i = 0; i < N; i++) {
X = arr[i];
if ((X & 1) != 0) {
Max = Math.Max(Max, Math.Abs(X));
Sum -= Math.Abs(X);
}
else {
Min = Math.Min(Min, Math.Abs(X));
Sum += Math.Abs(X);
}
}
if (Min >= Max)
return Sum;
return (Sum + 2 * (Max - Min));
}
public static void Main()
{
int [] arr
= { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.Write(maxSum(arr, N));
}
}
|
Javascript
<script>
function maxSum(arr,N)
{
let Max = Number.MIN_VALUE;
let Min = Number.MAX_VALUE;
let X, Sum = 0;
for (let i = 0; i < N; i++) {
X = arr[i];
if (X & 1) {
Max = Math.max(Max, Math.abs(X));
Sum -= Math.abs(X);
}
else {
Min = Math.min(Min, Math.abs(X));
Sum += Math.abs(X);
}
}
if (Min >= Max)
return Sum;
return (Sum + 2 * (Max - Min));
}
let arr = [ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 ];
let N = arr.length;
document.write(maxSum(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)