Maximize sum after K negations | Practice | GeeksforGeeks

Given an array of integers of size N and a number K., Your must modify array arr[] exactly K number of times. Here modify array means in each operation you can replace any array element either arr[i] by -arr[i] or -arr[i] by arr[i]. You need to perform this operation in such a way that after K operations, the sum of the array must be maximum.

Example 1:
Input:
N = 5, K = 1
arr[] = {1, 2, -3, 4, 5}
Output:
15
Explanation:
We have k=1 so we can change -3 to 3 and
sum all the elements to produce 15 as output.
Example 2:
Input:
N = 10, K = 5
arr[] = {5, -2, 5, -4, 5, -12, 5, 5, 5, 20}
Output:
68
Explanation:
Here  we have k=5 so we turn -2, -4, -12 to
2, 4, and 12 respectively. Since we have
performed 3 operations so k is now 2. To get
maximum sum of array we can turn positive
turned 2 into negative and then positive
again so k is 0. Now sum is
5+5+4+5+12+5+5+5+20+2 = 68

Your Task:
You don't have to print anything, print ting is done by the driver code itself. You have to complete the function maximizeSum() which takes the array A[], its size N, and an integer K as inputs and returns the maximum possible sum.

Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N,K ≤ 105
-109 ≤ Ai ≤ 109
0
kushwahdeepu5055in 4 hours
bhai mere pass problems ko disscuss karne ke liye koi achha friend nahi he mere ko coding me interest he 
 
bhai mene code discussion ke liye ek whatsapp group banaya he 
 
i invite you to guys please we only talk about codes and algos
 
https://chat.whatsapp.com/HZx1yFezIthBDDRkBdk5Xe
0
kushwahdeepu5055in 15 minutes
5 5 
1 2 3 4 5
iska correct ans 13 aayega yaa 14 mera ans 14 aa raha he joki wrong bata raha he plese help
 
0
skumarsahu55458 hours ago
C++ solution 
class Solution{   public:   long long int maximizeSum(long long int a[], int n, int k)   {       // Your code goes here       vector<int> arr;       for(int i=0; i<n; i++){           arr.push_back(a[i]);       }       sort(arr.begin(), arr.end());              int i=0;       while(k>0 && i<n){           if(arr[i]<0){               arr[i] = arr[i]*(-1);               k--;           }           i++;       }              if(k%2 != 0){           int min = *min_element(arr.begin(), arr.end());           auto it = find(arr.begin(), arr.end(), min);           int index = it - arr.begin();           arr[index] *= (-1);                  }              long long ans = 0;              for(int k=0; k<n; k++){           ans += arr[k];        }       return ans;   }};
0
akshaykumarmaurya4 days ago
// C++ code in easy way 
long long int maximizeSum(long long int a[], int n, int k)
    {
        // Your code goes here
        sort(a, a+n);
        int i;
        for(i=0;i<k;i++){
            
            
            if(0>a[i]){
                a[i] = -a[i];
                
            }
            else
                break;
                
        }
        long long int sum = 0;
        if((k-i)%2 == 0){
            
            for(int j=0;j<n;j++){
                sum+=a[j];
            
            }
            
            return sum;
        }
        
        sort(a, a+n);
        a[0] = -a[0];
        
        for(int i=0;i<n;i++){
                sum+=a[i];
        }
        return sum;
        
    }
+1
nitishmishra9371 week ago
Java | Heap
 
public static long maximizeSum(long a[], int n, int k)
    {
        PriorityQueue<Long> heap = new PriorityQueue<>();
        
        for(long e : a) {
            heap.add(e);
        }        
        
        if(heap.peek() > 0) {
            if(k == 1 || k % 2 != 0) {
                heap.add(heap.remove() * -1);
            }
        } else {
            for(int i = 0; i < k; ++i) {
                heap.add(heap.remove() * -1);
            }
        }
        
        int sum = 0;
        while(!heap.isEmpty()) {
            sum += heap.remove();
        }
        
        return sum;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.