Maximum array sum that can be obtained after exactly k changes in C++

C++Server Side ProgrammingProgramming

We are given with an array of positive and negative integers and a number K. The task is to find the maximum sum of the array after K changes in it’s elements. The single change operation here multiplies the single element by -1.

Approach used will be to convert every negative number to positive. If there are N negative numbers then, for this we will sort the array −

  • If N<K, after N operations every element will be positive, and we are left with K-N operations

  • Now if K-N is even then for remaining K-N operations changing signs will have no effect, do nothing.

  • If K-N is odd then for remaining K-N operations change the sign of least number (which will become negative) but the overall sum will be maximum.

    Or

    If N>K then change the sign of K negative numbers and add the array. Sum will be maximum.

Input

Arr[]= { 0,-2,6,4,8,2,-3 } K=4

Output

Maximum array sum is : 25

Explanation − The 4 changes in elements are

1. 0,2,6,4,8,2,-3 -2 changed to 2
2. 0,2,6,4,8,2,3 -3 changed to 3
3. 0,-2,6,4,8,2,3 2 changed to -2
4. 0,2,6,4,8,2,3 -2 changed to 2

Maximum sum is 25

Input

Arr[]= { -1,-2,-3,-4,-5,-6,-7 } K=4

Output

Maximum array sum is : 16

Explanation − The 4 changes in elements are

1. -1,-2,-3,-4,-5,-6,7 -7 changed to 7
2. -1,-2,-3,-4,-5,6,7 -6 changed to 6
3. -1,-2,-3,-4,5,6,7 -5 changed to 5
4. -1,-2,-3,4,5,6,7 -4 changed to 4
Maximum sum is 16

Approach used in the below program is as follows

  • The integer array Arr[] is used to store the integers.

  • Integer ‘size’ stores the length of the array and K is initialized.

  • Function returnSum( int arr[], int n, int k) takes an array , its size and k as input and returns the maximum sum of it’s elements after exactly k operations.

  • First of all we will sort the array using sort(arr,arr+n)

  • Now we will apply the operation arr[i]*-1 to all negative elements until either last index is reached or k becomes 0.

  • If k is smaller than the number of negative elements, then the above step will change k - ve elements.

  • If k is larger and the remaining value of k will be checked if it is odd or even.

  • If remaining k is odd then we will change the value of minimum element by applying operation arr[i]*-1 once,where arr[i] is found at least. ( multiplying by -1 odd times is same as doing it once)

  • If remaining k is even then arr[i]*-1 will have no effect. Do nothing.

  • Calculate the sum of the whole array and return the result.

Example

#include <bits/stdc++.h>
using namespace std;
int returnSum(int arr[], int n, int k){
   // Sort the array elements
   sort(arr, arr + n);
   // Change signs of the negative elements
   // starting from the smallest
   //this loop will change the sign of -ve elements
   //for each k one -ve element is turned positive
   for(i=0;i<n;i++)
      if(k>0 && arr[i]<0){
         arr[i]=arr[i]*-1;
   k--;
}
//if k is non zero and odd change sign of minimum element
//once as it is same as changing its sign odd times
if (k % 2 == 1) {
   int min = arr[0];
   int pos=0; //index of minimum element
   for (i = 1; i < n; i++)
      if (arr[i]<min){
         min = arr[i];
         pos=i;
      }
      arr[pos] *= -1;
   }
   int sum = 0;
   for (int i = 0; i < n; i++)
      sum += arr[i];
   return sum;
}
int main(){
   int Arr[] = { -3, 4, -3, 6, 8 };
   int size =5;
   int K = 4;
   cout <<"Maximum array sum that can be obtained after exactly k changes"
   returnSum(Arr, size, K) << endl;
   return 0;
}

Output

Maximum array sum that can be obtained after exactly k changes : 24
raja
Published on 28-Jul-2020 14:28:31
Advertisements