Similar Problems

Similar Problems not available

Maximize Sum Of Array After K Negations - Leetcode Solution

Companies:

LeetCode:  Maximize Sum Of Array After K Negations Leetcode Solution

Difficulty: Easy

Topics: greedy sorting array  

Problem description

Given an array A of integers, we must modify the array in the following way: we choose an integer K, and we replace the array’s ith element with -A[i], K times for all i. We repeat this process any number of times (possibly zero, with K = 0) until we get a modified array that has the maximum possible sum of its elements. Return the maximum sum of the modified array.

Example:

Input: A = [4,2,3], K = 1 Output: 5 Explanation: Choose K = 1, then the array becomes [-4,2,3], and the sum of the array is 5.

Solution

Approach: In order to maximize the sum of the array, we should change the sign of the smallest element K times. So, we find the smallest element and change its sign K times. We repeat this process until we have used up all K or all elements are positive.

Algorithm:

  1. Initialize a variable sum to hold the sum of the array.
  2. Sort the array in ascending order.
  3. Traverse the array and find the smallest element.
  4. If the smallest element is negative and K is not zero, change its sign, decrement K and add its absolute value to the sum.
  5. Repeat step 3-4 until the smallest element is positive or K is zero.
  6. If K is not zero and is odd, change the sign of the smallest positive element.
  7. Return the sum of the modified array.

Let's implement the solution in Python:

def largest_sum_after_k_negations(A, K): sum = 0

# sort the array in ascending order
A.sort()

# traverse the array and find the smallest element
i = 0
while i < len(A) and K != 0:
    if A[i] < 0:
        # change the sign of negative element
        A[i] = -A[i]
        K -= 1
    i += 1

# sum the modified array elements
for n in A:
    sum += n

# if K is not zero and is odd, change the sign of the smallest positive element
if K != 0 and K % 2 != 0:
    sum -= 2 * min(A)

return sum

test the solution

A = [4, 2, 3] K = 1 print(largest_sum_after_k_negations(A, K)) # Output: 5

In the above code, we first initialize a variable sum to hold the sum of the modified array. We sort the array in ascending order and traverse it to find the smallest element. If the element is negative and K is not zero, we change its sign, decrement K and add its absolute value to the sum. We repeat this process until the smallest element is positive or K is zero. Then, we sum the modified array elements. If K is not zero and is odd, we change the sign of the smallest positive element. Finally, we return the sum of the modified array.

Time and Space complexity: The time complexity of the above solution is O(nlogn) as we are sorting the array in ascending order. The space complexity of the solution is O(1) as we are not using any additional data structure except variables for storing the array and sum.

Maximize Sum Of Array After K Negations Solution Code

1