Given an integer array nums
and an integer k
, modify the array in the following way:
i
and replace nums[i]
with -nums[i]
.You should apply this process exactly k
times. You may choose the same index i
multiple times.
Return the largest possible sum of the array after modifying it in this way.
Example 1:
Input: nums = [4,2,3], k = 1 Output: 5 Explanation: Choose index 1 and nums becomes [4,-2,3].
Example 2:
Input: nums = [3,-1,0,2], k = 3 Output: 6 Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].
Example 3:
Input: nums = [2,-3,-1,5,-4], k = 2 Output: 13 Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].
Constraints:
1 <= nums.length <= 104
-100 <= nums[i] <= 100
1 <= k <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach means trying every possible combination of negating numbers in the collection. We want to find the combination that produces the highest total sum after negating some numbers.
Here's how the algorithm would work step-by-step:
def maximize_sum_after_k_negations_brute_force(numbers, k):
maximum_sum = float('-inf')
number_of_elements = len(numbers)
# Iterate through all possible combinations of negations.
for i in range(1 << number_of_elements):
temp_numbers = numbers[:]
negation_count = 0
# Determine which numbers to negate based on the bitmask.
for index in range(number_of_elements):
if (i >> index) & 1:
temp_numbers[index] *= -1
negation_count += 1
# If the number of negations is less than k, negate the smallest number until k is reached.
while negation_count < k:
# Find the index of the smallest absolute value element
minimum_absolute_index = 0
for index in range(1, number_of_elements):
if abs(temp_numbers[index]) < abs(temp_numbers[minimum_absolute_index]):
minimum_absolute_index = index
temp_numbers[minimum_absolute_index] *= -1
negation_count += 1
current_sum = sum(temp_numbers)
# Keep track of the maximum sum found so far.
maximum_sum = max(maximum_sum, current_sum)
return maximum_sum
The key is to realize that negating the smallest number repeatedly is the most efficient way to maximize the sum if we have remaining negations. We want to flip the most negative numbers to positive first, and handle edge cases carefully.
Here's how the algorithm would work step-by-step:
def maximize_sum_after_k_negations(numbers, allowed_negations):
if allowed_negations == 0:
return sum(numbers)
numbers.sort()
# Flip the most negative numbers to positive first.
for index in range(len(numbers)):
if numbers[index] < 0 and allowed_negations > 0:
numbers[index] = -numbers[index]
allowed_negations -= 1
# Re-sort after flipping negative numbers.
numbers.sort()
# If negations remain, and it's an odd number,
# negate the smallest number to minimize impact.
if allowed_negations % 2 != 0:
numbers[0] = -numbers[0]
return sum(numbers)
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately since there are no elements to sum. |
K is zero | Return the sum of the original array. |
Array contains only positive numbers and K is odd | Negate the smallest number in the array after sorting to minimize the reduction in sum. |
Array contains only positive numbers and K is even | Return the sum of the original array since an even number of negations will result in the original values. |
Array contains only negative numbers and K is odd | Negate the largest negative number (closest to zero) to maximize the sum. |
Array contains only negative numbers and K is even | Return the sum of the absolute values of the array elements. |
Array contains zeros and K is a positive number | If a zero exists, prioritize negating elements until K is zero or we reach the zero element, effectively ignoring extra negations on zero. |
K is larger than the array size | Reduce K modulo 2 after negating all negative numbers, then if K is still odd, negate the smallest element, otherwise stop. |