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
Constraints
- 1 โค nums.length โค 104
- 1 โค k โค 104
- -100 โค nums[i] โค 100