Maximize Array sum after changing sign of any elements for exactly M times
Given an array arr[] of size N and an integer M, the task is to find the maximum sum of the array after changing the sign of any elements in the array for exactly M times. It is allowed to change the sign of the same element multiple times.
Examples:
Input: arr[ ] = {-3, 7, -1, -5, -3}, M = 4
Output: 19
Explanation:
4 operations on the array can be performed as,
Operation 1: Change the sign of arr[0] -> {3, 7, -1, -5, -3}
Operation 2: Change the sign of arr[2] -> {3, 7, 1, -5, -3}
Operation 3: Change the sign of arr[3] -> {3, 7, 1, 5, -3}
Operation 4: Change the sign of arr[4] -> {3, 7, 1, 5, 3}
The maximum sum of array obtained is 19.
Input: arr[ ] = {-4, 2, 3, 1}, M = 3
Output: 10
Approach: To solve the problem, the main idea is to flip the smallest number of the array in each iteration. By doing so, the negative values will be changed to positive and the array sum will be maximized.
Follow the steps below to solve the problem:
- Initialize a min priority queue, say pq[], and push all the elements of the array arr[].
- Initialize a variable, say sum = 0, to store the maximum sum of the array.
- Iterate a while loop till M is greater than 0 and do the following:
- Pop from the priority queue and subtract it from the variable sum.
- Flip the sign of the popped element by multiplying it with -1 and add it to the sum.
- Push the new flipped element in the priority queue and subtract 1 from M.
- Finally, print the maximum sum stored in the variable sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaximumSumWithMflips(
int arr[], int N, int M)
{
priority_queue< int , vector< int >, greater< int > > pq;
int sum = 0;
for ( int i = 0; i < N; i++) {
pq.push(arr[i]);
sum += arr[i];
}
while (M--) {
sum -= pq.top();
int temp = -1 * pq.top();
pq.pop();
sum += temp;
pq.push(temp);
}
cout << sum;
}
int main()
{
int arr[] = { -3, 7, -1, -5, -3 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = 4;
findMaximumSumWithMflips(arr, N, M);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.lang.Math;
class GFG {
static void findMaximumSumWithMflips(
int arr[], int N, int M)
{
PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
minHeap.add(arr[i]);
sum += arr[i];
}
while (M-- > 0 ) {
sum -= minHeap.peek();
int temp = - 1 * minHeap.peek();
minHeap.remove();
sum += temp;
minHeap.add(temp);
}
System.out.println(sum);
}
public static void main(String[] args)
{
int arr[] = { - 3 , 7 , - 1 , - 5 , - 3 };
int M = 4 ,N= 5 ;
findMaximumSumWithMflips(arr, N, M);
}
}
|
Python3
def findMaximumSumWithMflips(arr, N, M):
pq = []
sum = 0
for i in range (N):
pq.append(arr[i])
sum + = arr[i]
pq.sort()
while (M> 0 ):
sum - = pq[ 0 ]
temp = - 1 * pq[ 0 ]
pq = pq[ 1 :]
sum + = temp
pq.append(temp)
pq.sort()
M - = 1
print ( sum )
if __name__ = = '__main__' :
arr = [ - 3 , 7 , - 1 , - 5 , - 3 ]
N = len (arr)
M = 4
findMaximumSumWithMflips(arr, N, M)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void findMaximumSumWithMflips( int [] arr, int N,
int M)
{
List< int > minHeap = new List< int >();
int sum = 0;
for ( int i = 0; i < N; i++) {
minHeap.Add(arr[i]);
sum += arr[i];
}
minHeap.Sort();
while (M-- > 0) {
sum -= minHeap[0];
int temp = -1 * minHeap[0];
minHeap.RemoveAt(0);
sum += temp;
minHeap.Add(temp);
minHeap.Sort();
}
Console.WriteLine(sum);
}
public static void Main(String[] args)
{
int [] arr = { -3, 7, -1, -5, -3 };
int M = 4, N = 5;
findMaximumSumWithMflips(arr, N, M);
}
}
|
Javascript
<script>
function findMaximumSumWithMflips(arr, N, M)
{
let pq = []
let sum = 0
for (let i = 0; i < N; i++){
pq.push(arr[i])
sum += arr[i]
pq.sort((a, b) => a - b)
}
while (M > 0){
sum -= pq[0]
temp = -1 * pq[0]
pq.shift()
sum += temp
pq.push(temp)
pq.sort((a, b) => a - b)
M -= 1
}
document.write(sum)
}
let arr = [-3, 7, -1, -5, -3]
let N = arr.length
let M = 4
findMaximumSumWithMflips(arr, N, M)
</script>
|
Time Complexity: O(NLogN)
Auxiliary Space: O(N)
Last Updated :
29 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...