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] <= 1001 <= k <= 104Approach 1: Sorting and Greedy Negation
This approach involves sorting the array first, which helps address the negativity in a prioritized fashion. By sorting, negative numbers are brought to the forefront. The plan is to turn all negative numbers positive for immediate gains. After flipping the necessary negatives or using up the flips, if flips remain, we aim them repeatedly at the smallest number in terms of absolute value to ensure no more productive use of a negation exists.
The C solution uses the standard qsort to sort the array. The core logic emphasizes negating the smallest negative numbers first, iterating through the array until either all negative numbers are converted or k negations are exhausted. Should negations remain post initial conversion, an odd count of k means a further single alteration is advantageous, specifically targeting the smallest number.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) as the sorting is done in-place.
Approach 2: Using Priority Queue (Min-Heap)
A priority queue (or min-heap) will provide an elegant method to always operate on the smallest element. The process involves inserting all numbers into a heap, then multiple negations individualizing the smallest available value until k operations are exhausted. This method efficiently manages both negation and retrieval of minimal elements.
The C++ implementation uses a min-heap, inserting all numbers initially. We invoke k negations, ensuring to log the negated value back into the heap, thus retaining a valid minimum tracking mechanism. The strategy processes/instruments flipping k times and sums up the remainder through a cumulative heap pop.
Python
Java
Time Complexity: O(n + k log n), where n is for heap initialization and k log n for operations.
Space Complexity: O(n) for the priority queue.
| Approach | Complexity |
|---|---|
| Approach 1: Sorting and Greedy Negation | Time Complexity: O(n log n) due to sorting. |
| Approach 2: Using Priority Queue (Min-Heap) | Time Complexity: O(n + k log n), where n is for heap initialization and k log n for operations. |
Subarray Sum Equals K - Prefix Sums - Leetcode 560 - Python • NeetCode • 266,725 views views
Watch 9 more video solutions →Practice Maximize Sum Of Array After K Negations with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor