Maximize Array sum by subtracting absolute of odd and adding absolute of even elements
Given an array arr[] of size N, the task is to maximize the array sum by subtracting the absolute values of all odd and adding and absolute values of all even elements, with only at most one exceptional even-odd pair, i.e., only one even value can be subtracted and one odd element can be added.
Examples:
Input: arr[] = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}
Output: 4
Explanation: Total absolute value subtracted = 5 + 3 + 1 + 1 + 3 + 5 = 18.
Total absolute value added = 4 + 2 + 0 + 2 + 4 = 12
Sum achieved = -6. Make {5, 0} the exceptional elements
Add 5 to sum and subtract 0 from sum.
So total value subtracted = 13 and total value added = 17
New Sum = 17 – 3 = 4. This is the maximum possible sum.Input: arr[] = {1, -2, 3}
Output: 0
Approach: The problem can be solved based on the following idea:
To maximize the sum, the maximum absolute value which is subtracted (say max) and the minimum absolute value which is added (say min) should be the exceptional elements. So the sum will increase by 2*(max – min).
Now, this should be performed only when min is less than max. Otherwise, the above term will become negative and will reduce the total sum.
Follow the below steps to solve the problem:
- Initialize the variables Max, Min, Sum to store maximum absolute odd, minimum absolute even and total sum respectively.
- Traverse the array from 0 to N – 1.
- If element is odd then subtract its absolute value from the Sum and update Max.
- If element is even then add its absolute value to the Sum and update Min.
- If Min is greater than Max then no need to update sum.
- Else, update the Sum, Sum = Sum + 2*(Max – Min).
Below is the implementation of the above approach:
C++
// C++ code for above approach #include <bits/stdc++.h> using namespace std; // Function to return maximum sum // after swapping int maxSum( int * arr, int N) { // Initialize the variables int Max = INT_MIN; int Min = INT_MAX; int X, Sum = 0; // Traverse the array for ( int i = 0; i < N; i++) { X = arr[i]; // If element is odd then subtract // it from the Sum and update maximum if (X & 1) { Max = max(Max, abs (X)); Sum -= abs (X); } // Else add it to the Sum and // update minimum else { Min = min(Min, abs (X)); Sum += abs (X); } } // If minimum even element is greater // than maximum odd element then no // need to swap if (Min >= Max) return Sum; // Else print the sum after swapping return (Sum + 2 * (Max - Min)); } // Driver Code int main() { int arr[] = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call cout << maxSum(arr, N); return 0; } |
Java
// Java code for above approach import java.io.*; class GFG { // Function to return maximum sum // after swapping public static int maxSum( int arr[], int N) { // Initialize the variables int Max = Integer.MIN_VALUE; int Min = Integer.MAX_VALUE; int X, Sum = 0 ; // Traverse the array for ( int i = 0 ; i < N; i++) { X = arr[i]; // If element is odd then subtract // it from the Sum and update maximum if ((X & 1 ) != 0 ) { Max = Math.max(Max, Math.abs(X)); Sum -= Math.abs(X); } // Else add it to the Sum and // update minimum else { Min = Math.min(Min, Math.abs(X)); Sum += Math.abs(X); } } // If minimum even element is greater // than maximum odd element then no // need to swap if (Min >= Max) return Sum; // Else print the sum after swapping return (Sum + ( 2 * (Max - Min))); } public static void main(String[] args) { int arr[] = { - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 }; int N = arr.length; // Function call System.out.print(maxSum(arr, N)); } } // This code is contributed by Rohit Pradhan. |
Python3
# Python code to implement the approach # Function to return maximum sum # after swapping import sys def maxSum(arr, N): # Initialize the variables Max = - sys.maxsize - 1 Min = sys.maxsize X, Sum = 0 , 0 # Traverse the array for i in range (N): X = arr[i] # If element is odd then subtract # it from the Sum and update maximum if (X & 1 ): Max = max ( Max , abs (X)) Sum - = abs (X) # Else add it to the Sum and # update minimum else : Min = min ( Min , abs (X)) Sum + = abs (X) # If minimum even element is greater # than maximum odd element then no # need to swap if ( Min > = Max ): return Sum # Else print the sum after swapping return ( Sum + 2 * ( Max - Min )) # Driver Code arr = [ - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 ] N = len (arr) # Function call print (maxSum(arr, N)) # This code is contributed by shinjanpatra |
C#
// C# code for above approach using System; class GFG { // Function to return maximum sum // after swapping static int maxSum( int [] arr, int N) { // Initialize the variables int Max = Int32.MinValue; int Min = Int32.MaxValue; int X, Sum = 0; // Traverse the array for ( int i = 0; i < N; i++) { X = arr[i]; // If element is odd then subtract // it from the Sum and update maximum if ((X & 1) != 0) { Max = Math.Max(Max, Math.Abs(X)); Sum -= Math.Abs(X); } // Else add it to the Sum and // update minimum else { Min = Math.Min(Min, Math.Abs(X)); Sum += Math.Abs(X); } } // If minimum even element is greater // than maximum odd element then no // need to swap if (Min >= Max) return Sum; // Else print the sum after swapping return (Sum + 2 * (Max - Min)); } // Driver Code public static void Main() { int [] arr = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; int N = arr.Length; // Function call Console.Write(maxSum(arr, N)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript implementation of the approach // Function to return maximum sum // after swapping function maxSum(arr,N) { // Initialize the variables let Max = Number.MIN_VALUE; let Min = Number.MAX_VALUE; let X, Sum = 0; // Traverse the array for (let i = 0; i < N; i++) { X = arr[i]; // If element is odd then subtract // it from the Sum and update maximum if (X & 1) { Max = Math.max(Max, Math.abs(X)); Sum -= Math.abs(X); } // Else add it to the Sum and // update minimum else { Min = Math.min(Min, Math.abs(X)); Sum += Math.abs(X); } } // If minimum even element is greater // than maximum odd element then no // need to swap if (Min >= Max) return Sum; // Else print the sum after swapping return (Sum + 2 * (Max - Min)); } // Driver Code let arr = [ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 ]; let N = arr.length; // Function call document.write(maxSum(arr, N)); // This code is contributed by shinjanpatraa </script> |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...