Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

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 ≤ 10^{5}

-10^{9} ≤ A_{i} ≤ 10^{9}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Maximize sum after K negations

...

Here is some troubleshooting advice:

- 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.

Here is some troubleshooting advice for contest problems

- 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.