|
| 1 | +--- |
| 2 | +id: maximize-sum-of-array-after-k-negations |
| 3 | +title: Maximize Sum of Array After K Negations |
| 4 | +sidebar_label: 1005-Maximize Sum of Array After K Negations |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Greedy |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Maximize Sum After K Negations problem on LeetCode." |
| 13 | +sidebar_position: 20 |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +Given an integer array `nums` and an integer `k`, modify the array in the following way: |
| 19 | + |
| 20 | +choose an index `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. |
| 21 | + |
| 22 | +Return the largest possible sum of the array after modifying it in this way. |
| 23 | + |
| 24 | +### Examples |
| 25 | + |
| 26 | +**Example 1:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: nums = [4,2,3], k = 1 |
| 30 | +Output: 5 |
| 31 | +Explanation: Choose index 1 and nums becomes [4,-2,3]. |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +``` |
| 37 | +Input: nums = [3,-1,0,2], k = 3 |
| 38 | +Output: 6 |
| 39 | +Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2]. |
| 40 | +``` |
| 41 | + |
| 42 | +**Example 3:** |
| 43 | + |
| 44 | +``` |
| 45 | +Input: nums = [2,-3,-1,5,-4], k = 2 |
| 46 | +Output: 13 |
| 47 | +Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4]. |
| 48 | +``` |
| 49 | + |
| 50 | +### Constraints |
| 51 | + |
| 52 | +- `1 <= nums.length <= 10^4` |
| 53 | +- `-100 <= nums[i] <= 100` |
| 54 | +- `1 <= k <= 10^4` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Solution for Largest Sum After K Negations Problem |
| 59 | + |
| 60 | +To solve this problem, we should follow a greedy approach by repeatedly negating the smallest element in the array, as this will maximize the sum after `k` operations. |
| 61 | + |
| 62 | +### Approach |
| 63 | + |
| 64 | +1. **Sort the Array:** |
| 65 | + - Sort the array to bring all the negative numbers to the front. |
| 66 | + |
| 67 | +2. **Negate the Minimum Element:** |
| 68 | + - Iterate through the array and negate the smallest elements (negative numbers) first. |
| 69 | + - If there are still operations left after negating all negative numbers, use the remaining operations on the smallest absolute value element. |
| 70 | + |
| 71 | +3. **Handle Remaining Operations:** |
| 72 | + - If `k` is still greater than 0 after negating all negative numbers, continue to negate the smallest absolute value element until `k` becomes 0. |
| 73 | + |
| 74 | +### Code in Different Languages |
| 75 | + |
| 76 | +<Tabs> |
| 77 | +<TabItem value="C++" label="C++" default> |
| 78 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 79 | + |
| 80 | +```cpp |
| 81 | +class Solution { |
| 82 | +public: |
| 83 | + int largestSumAfterKNegations(vector<int>& nums, int k) { |
| 84 | + sort(nums.begin(), nums.end()); |
| 85 | + for (int i = 0; i < nums.size() && k > 0; ++i) { |
| 86 | + if (nums[i] < 0) { |
| 87 | + nums[i] = -nums[i]; |
| 88 | + --k; |
| 89 | + } |
| 90 | + } |
| 91 | + if (k % 2 == 1) { |
| 92 | + *min_element(nums.begin(), nums.end()) *= -1; |
| 93 | + } |
| 94 | + return accumulate(nums.begin(), nums.end(), 0); |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
| 98 | +
|
| 99 | +</TabItem> |
| 100 | +<TabItem value="Java" label="Java"> |
| 101 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 102 | +
|
| 103 | +```java |
| 104 | +class Solution { |
| 105 | + public int largestSumAfterKNegations(int[] nums, int k) { |
| 106 | + Arrays.sort(nums); |
| 107 | + for (int i = 0; i < nums.length && k > 0; ++i) { |
| 108 | + if (nums[i] < 0) { |
| 109 | + nums[i] = -nums[i]; |
| 110 | + --k; |
| 111 | + } |
| 112 | + } |
| 113 | + int min = Arrays.stream(nums).min().getAsInt(); |
| 114 | + if (k % 2 == 1) { |
| 115 | + for (int i = 0; i < nums.length; ++i) { |
| 116 | + if (nums[i] == min) { |
| 117 | + nums[i] = -nums[i]; |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return Arrays.stream(nums).sum(); |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +</TabItem> |
| 128 | +<TabItem value="Python" label="Python"> |
| 129 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 130 | + |
| 131 | +```python |
| 132 | +class Solution: |
| 133 | + def largestSumAfterKNegations(self, nums: List[int], k: int) -> int: |
| 134 | + nums.sort() |
| 135 | + for i in range(len(nums)): |
| 136 | + if nums[i] < 0 and k > 0: |
| 137 | + nums[i] = -nums[i] |
| 138 | + k -= 1 |
| 139 | + if k % 2 == 1: |
| 140 | + nums[nums.index(min(nums))] *= -1 |
| 141 | + return sum(nums) |
| 142 | +``` |
| 143 | + |
| 144 | +</TabItem> |
| 145 | +</Tabs> |
| 146 | + |
| 147 | +#### Complexity Analysis |
| 148 | + |
| 149 | +- **Time Complexity**: $O(n \log n)$, where `n` is the length of the array. This is due to the sorting step. |
| 150 | +- **Space Complexity**: $O(1)$, as we are modifying the array in place. |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +<h2>Authors:</h2> |
| 155 | + |
| 156 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 157 | +{['ImmidiSivani'].map(username => ( |
| 158 | + <Author key={username} username={username} /> |
| 159 | +))} |
| 160 | +</div> |
| 161 | +``` |
0 commit comments