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
This problem has very simple solution, we just have 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.
C++
// C++ program to maximize array sum after // k operations. #include <bits/stdc++.h> using namespace std; // This function does k operations on array // in a way that maximize the array sum. // index --> stores the index of current minimum // element for j'th operation int maximumSum( int arr[], int n, int k) { // Modify array K number of times for ( int i = 1; i <= k; i++) { int min = INT_MAX; int index = -1; // Find minimum element in array for // current operation and modify it // i.e; arr[j] --> -arr[j] for ( int j = 0; j < n; j++) { if (arr[j] < min) { min = arr[j]; index = j; } } // this the condition if we find 0 as // minimum element, so it will useless to // replace 0 by -(0) for remaining operations if (min == 0) break ; // Modify element of array arr[index] = -arr[index]; } // Calculate sum of array int sum = 0; for ( int i = 0; i < n; i++) sum += arr[i]; return sum; } // Driver code int main() { int arr[] = { -2, 0, 5, -1, 2 }; int k = 4; int n = sizeof (arr) / sizeof (arr[0]); cout << maximumSum(arr, n, k); return 0; } |
Java
// Java program to maximize array // sum after k operations. class GFG { // This function does k operations // on array in a way that maximize // the array sum. index --> stores // the index of current minimum // element for j'th operation static int maximumSum( int arr[], int n, int k) { // Modify array K number of times for ( int i = 1 ; i <= k; i++) { int min = + 2147483647 ; int index = - 1 ; // Find minimum element in array for // current operation and modify it // i.e; arr[j] --> -arr[j] for ( int j = 0 ; j < n; j++) { if (arr[j] < min) { min = arr[j]; index = j; } } // this the condition if we find 0 as // minimum element, so it will useless to // replace 0 by -(0) for remaining operations if (min == 0 ) break ; // Modify element of array arr[index] = -arr[index]; } // Calculate sum of array int sum = 0 ; for ( int i = 0 ; i < n; i++) sum += arr[i]; return sum; } // Driver code public static void main(String arg[]) { int arr[] = { - 2 , 0 , 5 , - 1 , 2 }; int k = 4 ; int n = arr.length; System.out.print(maximumSum(arr, n, k)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to maximize # array sum after k operations. # This function does k operations on array # in a way that maximize the array sum. # index --> stores the index of current # minimum element for j'th operation def maximumSum(arr, n, k): # Modify array K number of times for i in range ( 1 , k + 1 ): min = + 2147483647 index = - 1 # Find minimum element in array for # current operation and modify it # i.e; arr[j] --> -arr[j] for j in range (n): if (arr[j] < min ): min = arr[j] index = j # this the condition if we find 0 as # minimum element, so it will useless to # replace 0 by -(0) for remaining operations if ( min = = 0 ): break # Modify element of array arr[index] = - arr[index] # Calculate sum of array sum = 0 for i in range (n): sum + = arr[i] return sum # Driver code arr = [ - 2 , 0 , 5 , - 1 , 2 ] k = 4 n = len (arr) print (maximumSum(arr, n, k)) # This code is contributed by Anant Agarwal. |
C#
// C# program to maximize array // sum after k operations. using System; class GFG { // This function does k operations // on array in a way that maximize // the array sum. index --> stores // the index of current minimum // element for j'th operation static int maximumSum( int [] arr, int n, int k) { // Modify array K number of times for ( int i = 1; i <= k; i++) { int min = +2147483647; int index = -1; // Find minimum element in array for // current operation and modify it // i.e; arr[j] --> -arr[j] for ( int j = 0; j < n; j++) { if (arr[j] < min) { min = arr[j]; index = j; } } // this the condition if we find // 0 as minimum element, so it // will useless to replace 0 by -(0) // for remaining operations if (min == 0) break ; // Modify element of array arr[index] = -arr[index]; } // Calculate sum of array int sum = 0; for ( int i = 0; i < n; i++) sum += arr[i]; return sum; } // Driver code public static void Main() { int [] arr = { -2, 0, 5, -1, 2 }; int k = 4; int n = arr.Length; Console.Write(maximumSum(arr, n, k)); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // PHP program to maximize // array sum after k operations. // This function does k operations // on array in a way that maximize // the array sum. index --> stores // the index of current minimum // element for j'th operation function maximumSum( $arr , $n , $k ) { $INT_MAX = 0; // Modify array K // number of times for ( $i = 1; $i <= $k ; $i ++) { $min = $INT_MAX ; $index = -1; // Find minimum element in // array for current operation // and modify it i.e; // arr[j] --> -arr[j] for ( $j = 0; $j < $n ; $j ++) { if ( $arr [ $j ] < $min ) { $min = $arr [ $j ]; $index = $j ; } } // this the condition if we // find 0 as minimum element, so // it will useless to replace 0 // by -(0) for remaining operations if ( $min == 0) break ; // Modify element of array $arr [ $index ] = - $arr [ $index ]; } // Calculate sum of array $sum = 0; for ( $i = 0; $i < $n ; $i ++) $sum += $arr [ $i ]; return $sum ; } // Driver Code $arr = array (-2, 0, 5, -1, 2); $k = 4; $n = sizeof( $arr ) / sizeof( $arr [0]); echo maximumSum( $arr , $n , $k ); // This code is contributed // by nitin mittal. ?> |
Output :
10
Time Complexity: O(k*n)
Auxiliary Space: O(1)
Approach 2 (Using Sort):
This approach is somewhat better than the above discussed method. In this method we will first sort the given array using java in-built sort function which has worst running time complexity of O(nlogn). Then for a given value of k we will continue to iterate through the array till k remains greater than 0, If value of array at any index is less than 0 we will change its sign and decrement k by 1. If we find a 0 in the array we will immediately set k equal to 0 to maximize our result. In some cases if we have all the values in array greater than 0 we will change sign of positive values, as our array is already sorted we will be changing signs of lower values present in the array which will eventually maximize our sum.
C++
// C++ program to find maximum array sum // after at most k negations. #include <bits/stdc++.h> using namespace std; int sol( int arr[], int n, int k) { int sum = 0; int i = 0; // Sorting given array using in-built // sort function sort(arr, arr + n); while (k > 0) { // If we find a 0 in our // sorted array, we stop if (arr[i] >= 0) k = 0; else { arr[i] = (-1) * arr[i]; k = k - 1; } i++; } // Calculating sum for ( int j = 0; j < n; j++) { sum += arr[j]; } return sum; } // Driver code int main() { int arr[] = { -2, 0, 5, -1, 2 }; int n = sizeof (arr) / sizeof (arr[0]); cout << sol(arr, n, 4) << endl; return 0; } // This code is contributed by pratham76 |
Java
// Java program to find maximum array sum // after at most k negations. import java.util.Arrays; public class GFG { static int sol( int arr[], int k) { // Sorting given array using in-built // java sort function Arrays.sort(arr); int sum = 0 ; int i = 0 ; while (k > 0 ) { // If we find a 0 in our // sorted array, we stop if (arr[i] >= 0 ) k = 0 ; else { arr[i] = (- 1 ) * arr[i]; k = k - 1 ; } i++; } // Calculating sum for ( int j = 0 ; j < arr.length; j++) { sum += arr[j]; } return sum; } // Driver Code public static void main(String[] args) { int arr[] = { - 2 , 0 , 5 , - 1 , 2 }; System.out.println(sol(arr, 4 )); } } |
Python3
# Python3 program to find maximum array # sum after at most k negations def sol(arr, k): # Sorting given array using # in-built java sort function arr.sort() Sum = 0 i = 0 while (k > 0 ): # If we find a 0 in our # sorted array, we stop if (arr[i] > = 0 ): k = 0 else : arr[i] = ( - 1 ) * arr[i] k = k - 1 i + = 1 # Calculating sum for j in range ( len (arr)): Sum + = arr[j] return Sum # Driver code arr = [ - 2 , 0 , 5 , - 1 , 2 ] print (sol(arr, 4 )) # This code is contributed by avanitrachhadiya2155 |
C#
// C# program to find maximum array sum // after at most k negations. using System; class GFG{ static int sol( int []arr, int k) { // Sorting given array using // in-built java sort function Array.Sort(arr); int sum = 0; int i = 0; while (k > 0) { // If we find a 0 in our // sorted array, we stop if (arr[i] >= 0) k = 0; else { arr[i] = (-1) * arr[i]; k = k - 1; } i++; } // Calculating sum for ( int j = 0; j < arr.Length; j++) { sum += arr[j]; } return sum; } // Driver code public static void Main( string [] args) { int []arr = { -2, 0, 5, -1, 2 }; Console.Write(sol(arr, 4)); } } // This code is contributed by rutvik_56 |
Output :
10
Time Complexity: O(n*logn)
Auxiliary Space: O(1)
Maximize array sum after K negations | Set 2
This article is contributed by Shashank Mishra ( Gullu ). 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.