Given an array of size n and a number k. We must modify array K number of times. Here modify array means in each operation we can replace any array element arr[i] by -arr[i]. We need to perform this operation in such a way that after K operations, sum of array must be maximum?
Examples:
Input : arr[] = {-2, 0, 5, -1, 2}
K = 4
Output: 10
// Replace (-2) by -(-2), array becomes {2, 0, 5, -1, 2}
// Replace (-1) by -(-1), array becomes {2, 0, 5, 1, 2}
// Replace (0) by -(0), array becomes {2, 0, 5, 1, 2}
// Replace (0) by -(0), array becomes {2, 0, 5, 1, 2}
Input : arr[] = {9, 8, 8, 5}
K = 3
Output: 20
We strongly recommend that you click here and practice it, before moving on to the solution.
We have discussed a O(nk) solution in below post.
Maximize array sum after K negations | Set 1
The idea used in above post is to replace the minimum element arr[i] in array by -arr[i] for current operation. In this way we can make sum of array maximum after K operations. Once interesting case is, once minimum element becomes 0, we don’t need to make any more changes.
The implementation used in above solution uses linear search to find minimum element. The time complexity of the above discussed solution is O(nk)
In this post an optimized solution is implemented that uses a priority queue (or binary heap) to find minimum element quickly.
Below is Java implementation of the idea. It uses PriorityQueue class in Java.
// A PriorityQueue based Java program to maximize array // sum after k negations. import java.util.*; class maximizeSum { public static int maxSum(int[] a, int k) { // Create a priority queue and insert all array elements // int PriorityQueue<Integer> pq = new PriorityQueue<>(); for (int x : a) pq.add(x); // Do k negations by removing a minimum element k times while (k-- > 0) { // Retrieve and remove min element int temp = pq.poll(); // Modify the minimum element and add back // to priority queue temp *= -1; pq.add(temp); } // Compute sum of all elements in priority queue. int sum = 0; for (int x : pq) sum += x; return sum; } // Driver code public static void main (String[] args) { int[] arr = {-2, 0, 5, -1, 2}; int k = 4; System.out.println(maxSum(arr, k)); } } |
Output:
10
Another Approach:(Efficient approach)
// A Java program to maximize array // sum after k negations. public class MaximizeSum { // function to maximize array // sum after k negations. static void maxSumAfterNegation(int[] a,int k){ int minPos = Integer.MAX_VALUE, sum = 0, index = -1; for(int i = 0; i < a.length; i++){ // Do k negations by removing a minimum element k times if(k < 0){ break; } if(a[i] < 0){ a[i]=-a[i]; k--; } if(a[i] < minPos && a[i] > -1){ minPos = a[i]; index = i; } // Compute sum of all elements sum+=a[i]; } for(int i = k; i > 0; i--){ a[index]=-a[index]; } /*for(int i:a){ sum+=i; }*/ sum+=(2*a[index]); System.out.print(sum); } //Driver code public static void main(String[] args) { // TODO Auto-generated method stub int[] a= {-2, 0, 5, -1, 2} ; MaximizeSum.maxSumAfterNegation(a, 4); } } |
Output:
10
Note that this optimized solution can be implemented in O(n + kLogn) time as we can create a priority queue (or binary heap) in O(n) time.
This article is contributed by Prakhar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Maximize array sum after K negations | Set 1
- Maximize the sum of array by multiplying prefix of array with -1
- Maximize value of (a[i]+i)*(a[j]+j) in an array
- Maximize value of (arr[i] - i) - (arr[j] - j) in an array
- Maximize the median of an array
- Given an array and three numbers, maximize (x * a[i]) + (y * a[j]) + (z * a[k])
- Rearrange an array to maximize i*arr[i]
- Maximize the bitwise OR of an array
- Maximize elements using another array
- Remove array end element to maximize the sum of product
- Maximize sum of consecutive differences in a circular array
- Maximize array elements upto given number
- Divide an array into k segments to maximize maximum of segment minimums
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Maximize arr[j] - arr[i] + arr[l] - arr[k], such that i < j < k < l
Improved By : AmanKumar63



