Maximizing array sum with given operation
There is an array consisting of (2 * n – 1) integers. We can change sign of exactly n elements in the array. In other words, we can select exactly n array elements, and multiply each of them by -1. Find the maximum sum of the array.
Examples :
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.
Approach:
- Step 1:- Iterate the loop for 2*n-1 times and repeat the steps 2, 3 and 4.
- Step 2:- Calculate no. of negative numbers (neg).
- Step 3:- Calculate the sum (sum) of the array by taking absolute values of the numbers.
- Step 4:- Find the minimum number of the array by taking absolute values of the numbers (min).
- Step 5:- Check if the no. of negative numbers is odd and the value of n (given) is even then subtract two times m from the sum and this will be max_sum of the array else, the value of sum will be the max_sum of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int arr[], int n)
{
int neg = 0, sum = 0,
m = INT_MAX, max_sum;
for ( int i = 0; i < 2 * n - 1; i++) {
neg += (arr[i] < 0);
sum += abs (arr[i]);
m = min(m, abs (arr[i]));
}
if (neg % 2 && n % 2 == 0) {
max_sum = sum -= 2 * m;
return (max_sum);
}
max_sum = sum;
return (max_sum);
}
int main()
{
int arr[] = { -1, -100, -1 };
int n = 2;
cout << maxSum(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static int maxSum( int arr[], int n) {
int neg = 0 , sum = 0 , m = 100000000 , max_sum;
for ( int i = 0 ; i < 2 * n - 1 ; i++) {
if (arr[i] < 0 )
neg += 1 ;
sum += Math.abs(arr[i]);
m = Math.min(m, Math.abs(arr[i]));
}
if (neg % 2 == 1 && n % 2 == 0 ) {
max_sum = sum -= 2 * m;
return (max_sum);
}
max_sum = sum;
return (max_sum);
}
public static void main(String args[]) {
int arr[] = {- 1 , - 100 , - 1 };
int n = 2 ;
System.out.println(maxSum(arr, n));
}
}
|
Python3
import sys
def maxSum (arr, n):
neg = 0
sum = 0
m = sys.maxsize
for i in range ( 2 * n - 1 ):
neg + = (arr[i] < 0 )
sum + = abs (arr[i])
m = min (m, abs (arr[i]))
if neg % 2 and n % 2 = = 0 :
max_sum = sum - 2 * m
return (max_sum)
max_sum = sum
return max_sum
arr = [ - 1 , - 100 , - 1 ]
n = 2
print ( maxSum(arr, n))
|
C#
using System;
class GFG
{
static int maxSum( int []arr, int n)
{
int neg = 0, sum = 0;
int m = 100000000, max_sum;
for ( int i = 0; i < 2 * n - 1; i++)
{
if (arr[i] < 0)
neg += 1;
sum += Math.Abs(arr[i]);
m = Math.Min(m, Math.Abs(arr[i]));
}
if (neg % 2 == 1 && n % 2 == 0)
{
max_sum = sum -= 2 * m;
return (max_sum);
}
max_sum = sum;
return (max_sum);
}
public static void Main()
{
int []arr = {-1, -100, -1};
int n = 2;
Console.WriteLine(maxSum(arr, n));
}
}
|
PHP
<?php
function maxSum( $arr , $n )
{
$neg = 0; $sum = 0;
$m = PHP_INT_MAX; $max_sum ;
for ( $i = 0; $i < 2 * $n - 1; $i ++)
{
$neg += ( $arr [ $i ] < 0);
$sum += abs ( $arr [ $i ]);
$m = min( $m , abs ( $arr [ $i ]));
}
if ( $neg % 2 && $n % 2 == 0)
{
$max_sum = $sum -= 2 * $m ;
return ( $max_sum );
}
$max_sum = $sum ;
return ( $max_sum );
}
$arr = array (-1, -100, -1);
$n = 2;
echo maxSum( $arr , $n ) ;
?>
|
Javascript
<script>
function maxSum(arr, n)
{
let neg = 0, sum = 0, m = Number.MAX_VALUE, max_sum;
for (let i = 0; i < 2 * n - 1; i++) {
neg += (arr[i] < 0);
sum += Math.abs(arr[i]);
m = Math.min(m, Math.abs(arr[i]));
}
if (neg % 2 && n % 2 == 0) {
max_sum = sum -= 2 * m;
return (max_sum);
}
max_sum = sum;
return (max_sum);
}
let arr = [ -1, -100, -1 ];
let n = 2;
document.write(maxSum(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
25 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...