Maximize array sum by alternating the signs of adjacent elements
Last Updated :
23 Apr, 2021
Given an array, arr[] of size N, the task is to find the maximum possible sum of array elements by alternating the signs of adjacent array elements.
Examples:
Input: arr[] = { -2, 1, 0 }
Output: 3
Explanation:
Alternating the signs of (arr[0], arr[1]) modifies arr[] to {2, -1, 0}.
Alternating the signs of (arr[1], arr[2]) modifies arr[] to {2, 1, 0}.
Therefore, the required output = (2 + 1 + 0) = 3, which is the maximum sum possible.
Input: arr[] = { 1, 1, -2, -4, 5 }
Output: 13
Explanation:
Alternating the signs of (arr[2], arr[3]) modifies arr[] to { 1, 1, 2, 4, 5 }
Therefore, the required output = (1 + 1 + 2 + 4 + 5) = 13, which is the maximum sum possible.
Approach: The problem can be solved using Greedy technique. The idea is based on the fact that the maximum count of negative elements in the array after alternating the signs of adjacent elements can’t be greater than 1. Follow the steps below to solve the problem:
- Initialize a variable, say MaxAltSum, to store the maximum possible sum of array elements by alternating the signs of adjacent elements.
- Traverse the array and count the number of negative elements in the array.
- If count of negative elements in the array is even, then maximum possible sum possible by alternating the signs of adjacent array elements is equal to the sum of absolute value of array elements, i.e. MaxAltSum = ?abs(arr[i])
- Otherwise, maximum possible sum obtained from the array by alternating the signs of adjacent array elements equal to the sum of the absolute value of all possible array elements, except the smallest absolute value of array elements. i.e, MaxAltSum = ((?abs(arr[i])) – 2 * X), where X is the smallest absolute value of array elements.
- Finally, print the value MaxAltSum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxSumByAlternatingSign( int arr[], int N)
{
int cntNeg = 0;
int MaxAltSum = 0;
int SmValue = 0;
int sum = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] < 0) {
cntNeg += 1;
}
sum += abs (arr[i]);
SmValue = min(SmValue,
abs (arr[i]));
}
MaxAltSum = sum;
if (cntNeg & 1) {
MaxAltSum -= 2 * SmValue;
}
return MaxAltSum;
}
int main()
{
int arr[] = { 1, 1, -2, -4, 5 };
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << findMaxSumByAlternatingSign(
arr, N);
}
|
Java
import java.util.*;
class GFG{
static int findMaxSumByAlternatingSign( int arr[],
int N)
{
int cntNeg = 0 ;
int MaxAltSum = 0 ;
int SmValue = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] < 0 )
{
cntNeg += 1 ;
}
sum += Math.abs(arr[i]);
SmValue = Math.min(SmValue,
Math.abs(arr[i]));
}
MaxAltSum = sum;
if (cntNeg % 2 == 1 )
{
MaxAltSum -= 2 * SmValue;
}
return MaxAltSum;
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , - 2 , - 4 , 5 };
int N = arr.length;
System.out.print(findMaxSumByAlternatingSign(
arr, N));
}
}
|
Python3
def findMaxSumByAlternatingSign(arr, N):
cntNeg = 0
MaxAltSum = 0
SmValue = 0
sum = 0
for i in range (N):
if (arr[i] < 0 ):
cntNeg + = 1
sum + = abs (arr[i])
SmValue = min (SmValue, abs (arr[i]))
MaxAltSum = sum
if (cntNeg & 1 ):
MaxAltSum - = 2 * SmValue
return MaxAltSum
if __name__ = = '__main__' :
arr = [ 1 , 1 , - 2 , - 4 , 5 ]
N = len (arr)
print (findMaxSumByAlternatingSign(arr, N))
|
C#
using System;
class GFG{
static int findMaxSumByAlternatingSign( int []arr,
int N)
{
int cntNeg = 0;
int MaxAltSum = 0;
int SmValue = 0;
int sum = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] < 0)
{
cntNeg += 1;
}
sum += Math.Abs(arr[i]);
SmValue = Math.Min(SmValue,
Math.Abs(arr[i]));
}
MaxAltSum = sum;
if (cntNeg % 2 == 1)
{
MaxAltSum -= 2 * SmValue;
}
return MaxAltSum;
}
public static void Main(String[] args)
{
int []arr = { 1, 1, -2, -4, 5 };
int N = arr.Length;
Console.Write(findMaxSumByAlternatingSign(
arr, N));
}
}
|
Javascript
<script>
function findMaxSumByAlternatingSign(arr, N)
{
let cntNeg = 0;
let MaxAltSum = 0;
let SmValue = 0;
let sum = 0;
for (let i = 0; i < N; i++)
{
if (arr[i] < 0)
{
cntNeg += 1;
}
sum += Math.abs(arr[i]);
SmValue = Math.min(SmValue,
Math.abs(arr[i]));
}
MaxAltSum = sum;
if (cntNeg % 2 == 1)
{
MaxAltSum -= 2 * SmValue;
}
return MaxAltSum;
}
let arr = [ 1, 1, -2, -4, 5 ];
let N = arr.length;
document.write(findMaxSumByAlternatingSign(
arr, N));
</script>
|
Output:
13
Time Complexity: O(N)
Auxiliary Space:O(1)
Share your thoughts in the comments
Please Login to comment...