Program to find maximum sum by performing at most k negate operations in Python

Suppose we have a list of elements called nums and another value k. We can perform an operation where we select an element from nums and negate it. We can perform exactly k number of operations and need to find the maximum resulting sum.

So, if the input is like nums = [2, 1, -6, -2] and k = 3, then the output will be 9. If we negate -6 and -2 and 1, we get [2, -1, 6, 2] and its sum is 9.

Algorithm

To solve this problem, we follow these steps −

  • Sort the array to process negative numbers first

  • Use k operations to negate the most negative numbers

  • If k operations remain and k is odd, negate the smallest absolute value

  • Return the sum of the modified array

Example

Let us see the implementation to get better understanding −

def solve(nums, k):
    n = len(nums)
    if n == 0:
        return 0
    
    # Sort to get negative numbers first
    nums.sort()
    
    # Use k operations to negate negative numbers
    for idx in range(n):
        if nums[idx] < 0 and k > 0:
            k -= 1
            nums[idx] *= -1
    
    # If k is odd, we need to negate the smallest number
    if k % 2 == 1:
        return sum(nums) - 2 * min(nums)
    
    return sum(nums)

# Test the function
nums = [2, 1, -6, -2]
k = 3
result = solve(nums, k)
print(f"Maximum sum after {k} operations: {result}")
Maximum sum after 3 operations: 9

How It Works

The algorithm works by first sorting the array to prioritize negative numbers. We negate the most negative numbers first to maximize the sum increase. If operations remain after negating all negative numbers and the remaining count is odd, we subtract twice the minimum value (effectively negating the smallest positive number).

# Step-by-step example
nums = [2, 1, -6, -2]
k = 3
print(f"Original array: {nums}")

# After sorting
nums.sort()
print(f"After sorting: {nums}")

# Simulate the negation process
for i in range(len(nums)):
    if nums[i] < 0 and k > 0:
        print(f"Negating {nums[i]} at position {i}")
        nums[i] *= -1
        k -= 1
        print(f"Array now: {nums}, remaining k: {k}")

print(f"Final sum: {sum(nums)}")
Original array: [2, 1, -6, -2]
After sorting: [-6, -2, 1, 2]
Negating -6 at position 0
Array now: [6, -2, 1, 2], remaining k: 2
Negating -2 at position 1
Array now: [6, 2, 1, 2], remaining k: 1
Final sum: 11

Key Points

  • Sorting ensures we negate the most negative numbers first

  • If k operations remain after negating all negatives, check if k is odd

  • When k is odd, we effectively negate the smallest positive number by subtracting twice its value

  • Time complexity is O(n log n) due to sorting

Conclusion

This greedy approach maximizes the sum by prioritizing the negation of the most negative numbers first. The key insight is handling remaining operations when k is odd after all negative numbers are processed.

Updated on: 2026-03-26T15:16:27+05:30

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements