class Solution:
def largestSumAfterKNegations(self, nums, k):
nums.sort()
i = 0
# Flip as many negative numbers as possible
while i < len(nums) and k > 0 and nums[i] < 0:
nums[i] = -nums[i]
i += 1
k -= 1
# If k is still odd, flip the smallest absolute value
if k % 2 == 1:
nums.sort()
nums[0] = -nums[0]
return sum(nums)
class Solution {
public:
int largestSumAfterKNegations(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int i = 0;
while (i < nums.size() && k > 0 && nums[i] < 0) {
nums[i] = -nums[i];
++i;
--k;
}
sort(nums.begin(), nums.end());
if (k % 2 == 1) {
nums[0] = -nums[0];
}
int sum = 0;
for (int n : nums) sum += n;
return sum;
}
};
import java.util.*;
class Solution {
public int largestSumAfterKNegations(int[] nums, int k) {
Arrays.sort(nums);
int i = 0;
while (i < nums.length && k > 0 && nums[i] < 0) {
nums[i] = -nums[i];
i++;
k--;
}
Arrays.sort(nums);
if (k % 2 == 1) {
nums[0] = -nums[0];
}
int sum = 0;
for (int n : nums) sum += n;
return sum;
}
}
var largestSumAfterKNegations = function(nums, k) {
nums.sort((a, b) => a - b);
let i = 0;
while (i < nums.length && k > 0 && nums[i] < 0) {
nums[i] = -nums[i];
i++;
k--;
}
nums.sort((a, b) => a - b);
if (k % 2 === 1) {
nums[0] = -nums[0];
}
return nums.reduce((a, b) => a + b, 0);
};
You are given an integer array nums
and an integer k
. You can choose any element from nums
and flip its sign (i.e., change nums[i]
to -nums[i]
). You can repeat this operation exactly k
times, and you may choose the same element multiple times.
Your task is to maximize the sum of the array after performing k
sign-flip operations. Return the largest possible sum.
k
flips.
At first glance, it might seem tempting to try all possible combinations of flips, but this would be very inefficient, especially for large arrays or large values of k
.
The key insight is that flipping a negative number to positive increases the sum more than flipping a positive to negative (which decreases the sum). Therefore, we should prioritize flipping the most negative numbers first.
Once all negatives are flipped, if we still have flips left, flipping the smallest (in absolute value) element is optimal for any remaining flips, especially if k
is odd.
By sorting the array and working from the smallest values upward, we can systematically maximize the sum in the minimum number of steps.
This approach ensures that each flip contributes maximally to increasing the sum, and any leftover flips are used optimally.
Suppose nums = [-4, -2, -3, 4, 3]
and k = 4
.
[-4, -3, -2, 3, 4]
k=3
): [4, -3, -2, 3, 4]k=2
): [4, 3, -2, 3, 4]k=1
): [4, 3, 2, 3, 4]k=1
):
The maximum possible sum after 4 flips is 12.
k
.
The elegant solution to maximizing the sum after k
negations is to flip all negative numbers first, then use any remaining flips on the smallest absolute value if needed. This is efficiently achieved by sorting the array and working through it linearly, yielding an O(n log n) algorithm that is both simple and powerful.
The key insight is that flipping negatives gives the biggest gain, and any leftover (odd) flips should be used on the smallest element to minimize loss. This strategy ensures the sum is as large as possible after exactly k
operations.