- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

- Related Articles
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Build Array Where You Can Find The Maximum Exactly K Comparisons in C++
- Maximize array sum after K negation in C++
- Maximum elements that can be made equal with k updates in C++
- Find maximum points which can be obtained by deleting elements from array in C++
- Maximum of smallest possible area that can get with exactly k cut of given rectangular in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find maximum points which can be obtained by deleting elements from array in Python
- K-Concatenation Maximum Sum in C++
- maximum length of continuous silk thread that can be obtained from a cocoon."
- Maximum subarray sum in an array created after repeated concatenation in C++
- Maximize Sum Of Array After K Negations in Python
- Maximum subarray sum in array formed by repeating the given array k times in C++
- Maximum subarray sum in an array created after repeated concatenation in C++ Program
- Find prime number K in an array such that (A[i] % K) is maximum in C++