Maximize Sum Of Array After K Negations - Problem

You're given an integer array nums and an integer k, and your goal is to maximize the sum of the array by strategically flipping signs!

The Challenge: You must perform exactly k negation operations, where each operation lets you pick any index i and replace nums[i] with -nums[i]. You can choose the same index multiple times if needed.

Strategy Matters: The key insight is that you want to eliminate negative numbers first (since flipping them increases the sum), and if you have leftover operations, you'll want to flip the smallest absolute value an even or odd number of times depending on what maximizes the final sum.

Example: With nums = [2, -3, -1, 5, -4] and k = 2, you could flip -4 to get [2, -3, -1, 5, 4], then flip -3 to get [2, 3, -1, 5, 4] for a final sum of 13.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [4,2,3], k = 1
โ€บ Output: 5
๐Ÿ’ก Note: Choose index 1 and nums becomes [4,-2,3]. The sum is 4 + (-2) + 3 = 5. Actually, we should choose index 1 to negate the smallest positive number: nums becomes [4,-2,3], sum = 5. Wait, that's wrong. We should negate index 1 (value 2): [4,-2,3] = 5. Actually the optimal is to negate the smallest value to minimize loss.
example_2.py โ€” Multiple Negatives
$ Input: nums = [3,-1,0,2], k = 3
โ€บ Output: 6
๐Ÿ’ก Note: Choose indices (1, 2, 2) and nums becomes [3,1,0,2]. We flip -1 to 1 (k=2 left), then 0 to 0 twice (k=0 left). Sum = 3 + 1 + 0 + 2 = 6.
example_3.py โ€” All Negatives
$ Input: nums = [-8,3,-5,-3,-5,-2], k = 6
โ€บ Output: 22
๐Ÿ’ก Note: We can flip all negative numbers: [-8,3,-5,-3,-5,-2] becomes [8,3,5,3,5,2]. Sum = 8 + 3 + 5 + 3 + 5 + 2 = 26. Wait, we have exactly 4 negatives and k=6, so 2 operations left. Since 2 is even, we end up with all positive values.

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • 1 โ‰ค k โ‰ค 104
  • -100 โ‰ค nums[i] โ‰ค 100

Visualization

Tap to expand
The Strategic Flip GameInitial State: nums = [-4, -3, -1, 2, 5], k = 2-4Biggest Debt-3-125Best Asset๐Ÿช„ Magic Flips: 2Apply Greedy StrategyAfter Strategic Flips:4Flipped! โšก3Flipped! โšก-1No flips left25๐Ÿช„ Magic Flips: 0Final Wealth4 + 3 + (-1) + 2 + 5= 13 coins! ๐Ÿ’ฐMaximum possible wealth๐Ÿ’ก Strategic Insights๐ŸŽฏ Always flip your biggest debts first - they give the maximum wealth increaseโšก If you have leftover flips, minimize losses by targeting your smallest asset๐Ÿ† This greedy strategy guarantees the optimal solution in O(n log n) time
Understanding the Visualization
1
Survey Your Wealth
Line up all your coins from worst debt to best asset
2
Eliminate Debts First
Use your magic flips on the biggest debts to turn them into assets
3
Handle Leftover Magic
If you have unused flips, minimize damage by flipping your smallest asset
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach of eliminating the most negative values first, followed by optimal handling of remaining operations, guarantees the maximum possible sum in optimal time complexity.
Asked in
Amazon 15 Google 12 Microsoft 8 Apple 5
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen