Maximum array sum that can be obtained after exactly k changes
Last Updated :
09 Jun, 2022
Given an array arr[] of n integers and an integer k. The task is to maximize the sum of the array after performing the given operation exactly k times. In a single operation, any element of the array can be multiplied by -1 i.e. the sign of the element can be changed.
Examples:
Input: arr[] = {-5, 4, 1, 3, 2}, k = 4
Output: 13
Change the sign of -5 once and then change the sign of 1 three times.
Thus, it changes to -1 and the sum will be 5 + 4 + (-1) + 3 + 2 = 13.
Input: arr[] = {-1, -1, 1}, k = 1
Output: 1
Approach: If the count of negative elements in the array is count,
- If count ? k then the sign of exactly k negative numbers will be changed starting from the smallest.
- If count < k then change the sign of all the negative elements to positive and for the remaining operations i.e. rem = k - count,
- If rem % 2 = 0 then no changes will be done to the current array as changing the sign of an element twice gives the original number.
- If rem % 2 = 1 then change the sign of the smallest element from the updated array.
- Finally, print the sum of the elements of the updated array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Utility function to return the sum
// of the array elements
int sumArr(int arr[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Function to return the maximized sum
// of the array after performing
// the given operation exactly k times
int maxSum(int arr[], int n, int k)
{
// Sort the array elements
sort(arr, arr + n);
int i = 0;
// Change signs of the negative elements
// starting from the smallest
while (i < n && k > 0 && arr[i] < 0) {
arr[i] *= -1;
k--;
i++;
}
// If a single operation has to be
// performed then it must be performed
// on the smallest positive element
if (k % 2 == 1) {
// To store the index of the
// minimum element
int min = 0;
for (i = 1; i < n; i++)
// Update the minimum index
if (arr[min] > arr[i])
min = i;
// Perform remaining operation
// on the smallest element
arr[min] *= -1;
}
// Return the sum of the updated array
return sumArr(arr, n);
}
// Driver code
int main()
{
int arr[] = { -5, 4, 1, 3, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 4;
cout << maxSum(arr, n, k) << endl;
return 0;
}
Java
// Java implementation of the above approach
import java.util.Arrays;
class GFG
{
// Utility function to return the sum
// of the array elements
static int sumArr(int arr[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Function to return the maximized sum
// of the array after performing
// the given operation exactly k times
static int maxSum(int arr[], int n, int k)
{
// Sort the array elements
Arrays.sort(arr);
int i = 0;
// Change signs of the negative elements
// starting from the smallest
while (i < n && k > 0 && arr[i] < 0)
{
arr[i] *= -1;
k--;
i++;
}
// If a single operation has to be
// performed then it must be performed
// on the smallest positive element
if (k % 2 == 1)
{
// To store the index of the
// minimum element
int min = 0;
for (i = 1; i < n; i++)
// Update the minimum index
if (arr[min] > arr[i])
min = i;
// Perform remaining operation
// on the smallest element
arr[min] *= -1;
}
// Return the sum of the updated array
return sumArr(arr, n);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { -5, 4, 1, 3, 2 };
int n = arr.length;
int k = 4;
System.out.println(maxSum(arr, n, k));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python 3 implementation of the approach
# Utility function to return the sum
# of the array elements
def sumArr(arr, n):
sum = 0
for i in range(n):
sum += arr[i]
return sum
# Function to return the maximized sum
# of the array after performing
# the given operation exactly k times
def maxSum(arr, n, k):
# Sort the array elements
arr.sort(reverse = False)
i = 0
# Change signs of the negative elements
# starting from the smallest
while (i < n and k > 0 and arr[i] < 0):
arr[i] *= -1
k -= 1
i += 1
# If a single operation has to be
# performed then it must be performed
# on the smallest positive element
if (k % 2 == 1):
# To store the index of the
# minimum element
min = 0
for i in range(1, n):
# Update the minimum index
if (arr[min] > arr[i]):
min = i
# Perform remaining operation
# on the smallest element
arr[min] *= -1
# Return the sum of the updated array
return sumArr(arr, n)
# Driver code
if __name__ == '__main__':
arr = [-5, 4, 1, 3, 2]
n = len(arr)
k = 4
print(maxSum(arr, n, k))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the above approach
using System;
using System.Linq;
class GFG
{
// Utility function to return the sum
// of the array elements
static int sumArr(int [] arr, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Function to return the maximized sum
// of the array after performing
// the given operation exactly k times
static int maxSum(int [] arr, int n, int k)
{
// Sort the array elements
Array.Sort(arr);
int i = 0;
// Change signs of the negative elements
// starting from the smallest
while (i < n && k > 0 && arr[i] < 0)
{
arr[i] *= -1;
k--;
i++;
}
// If a single operation has to be
// performed then it must be performed
// on the smallest positive element
if (k % 2 == 1)
{
// To store the index of the
// minimum element
int min = 0;
for (i = 1; i < n; i++)
// Update the minimum index
if (arr[min] > arr[i])
min = i;
// Perform remaining operation
// on the smallest element
arr[min] *= -1;
}
// Return the sum of the updated array
return sumArr(arr, n);
}
// Driver code
static void Main()
{
int []arr= { -5, 4, 1, 3, 2 };
int n = arr.Length;
int k = 4;
Console.WriteLine(maxSum(arr, n, k));
}
}
// This code is contributed by mohit kumar 29
PHP
<?php
// PHP implementation of the approach
// Utility function to return the sum
// of the array elements
function sumArr($arr, $n)
{
$sum = 0;
for ($i = 0; $i < $n; $i++)
$sum += $arr[$i];
return $sum;
}
// Function to return the maximized sum
// of the array after performing
// the given operation exactly k times
function maxSum($arr, $n, $k)
{
// Sort the array elements
sort($arr);
$i = 0;
// Change signs of the negative elements
// starting from the smallest
while ($i < $n && $k > 0 &&
$arr[$i] < 0)
{
$arr[$i] *= -1;
$k--;
$i++;
}
// If a single operation has to be
// performed then it must be performed
// on the smallest positive element
if ($k % 2 == 1)
{
// To store the index of the
// minimum element
$min = 0;
for ($i = 1; $i < $n; $i++)
// Update the minimum index
if ($arr[$min] > $arr[$i])
$min = $i;
// Perform remaining operation
// on the smallest element
$arr[$min] *= -1;
}
// Return the sum of the updated array
return sumArr($arr, $n);
}
// Driver code
$arr = array( -5, 4, 1, 3, 2 );
$n = sizeof($arr);
$k = 4;
echo maxSum($arr, $n, $k), "\n";
// This code is contributed by ajit.
?>
JavaScript
<script>
// Javascript implementation of the above approach
// Utility function to return the sum
// of the array elements
function sumArr(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Function to return the maximized sum
// of the array after performing
// the given operation exactly k times
function maxSum(arr, n, k)
{
// Sort the array elements
arr.sort(function(a, b){return a - b});
let i = 0;
// Change signs of the negative elements
// starting from the smallest
while (i < n && k > 0 && arr[i] < 0)
{
arr[i] *= -1;
k--;
i++;
}
// If a single operation has to be
// performed then it must be performed
// on the smallest positive element
if (k % 2 == 1)
{
// To store the index of the
// minimum element
let min = 0;
for (i = 1; i < n; i++)
// Update the minimum index
if (arr[min] > arr[i])
min = i;
// Perform remaining operation
// on the smallest element
arr[min] *= -1;
}
// Return the sum of the updated array
return sumArr(arr, n);
}
let arr= [ -5, 4, 1, 3, 2 ];
let n = arr.length;
let k = 4;
document.write(maxSum(arr, n, k));
</script>
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Similar Reads
Maximum sum of pairs that are at least K distance apart in an array Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum sum of pairs of elements that are separated by at least K indices. Examples: Input: arr[] = {2, 4, 1, 6, 8}, K = 2Output: 12Explanation:The elements {1, 4} are K(= 2) distance apart. The sum of pairs {4,
6 min read
Maximum path sum in the given arrays with at most K jumps Given three arrays A, B, and C each having N elements, the task is to find the maximum sum that can be obtained along any valid path with at most K jumps.A path is valid if it follows the following properties: It starts from the 0th index of an array.It ends at (N-1)th index of an array.For any elem
15+ min read
Maximum subsequence sum with adjacent elements having atleast K difference in index Given an array arr[] consisting of integers of length N and an integer K (1 ? k ? N), the task is to find the maximum subsequence sum in the array such that adjacent elements in that subsequence have at least a difference of K in their indices in the original array. Examples: Input: arr[] = {1, 2, -
8 min read
Remove k corner elements to maximize remaining sum Given an array, the task is to remove total k elements from corners to maximize the sum of remaining elements. For example, if we k = 5 and if we remove 2 elements from the left corner, then we need to remove 3 elements from the right corner.Examples: Input : arr = [11, 49, 100, 20, 86, 29, 72], k =
7 min read
Find maximum Subsequence Sum according to given conditions Given an integer array nums and an integer K, The task is to find the maximum sum of a non-empty subsequence of the array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= K is satisfied. A subsequence of an array is obtai
5 min read