Maximum sum of all elements of array after performing given operations
Given an array of integers. The task is to find the maximum sum of all the elements of the array after performing the given two operations once each.
The operations are:
1. Select some(possibly none) continuous elements from the beginning of the array and multiply by -1.
2. Select some(possibly none) continuous elements from the end of the array and multiply by -1.
Examples:
Input : arr[] = {-1, 10, -5, 10, -2} Output : 18 After 1st operation : 1 10 -5 10 -2 After 2nd operation : 1 10 -5 10 2 Input : arr[] = {-9, -8, -7} Output : 24 After 1st operation : 9 8 -7 After 2nd operation : 9 8 7
Approach: This problem can be solved in linear time, using the following idea:
- Let the sum of elements A1 .. An be equal to S. Then when inverting signs we get -A1, -A2 .. -An, and the sum is thereafter changed to -S, i.e. sum of elements on the segment will just change its’ sign when inverting the whole segment’s signs.
- Consider the initial problem as follows: choose a consecutive subsequence, and invert all the numbers remaining out of it.
- Find the Maximum subarray sum using Kadane’ Algorithm.
- Keep that subarray intact and multiply the rest with -1.
- Considering the sum of the whole array as S, and the largest sum contiguous subarray as S1, the total sum will be equal to -(S-S1) + S1 = 2*S1 – S. This is the required sum.
Below is the implementation of the above approach:
C++
// CPP program to find the maximum // sum after given operations #include <bits/stdc++.h> using namespace std; // Function to calculate Maximum Subarray Sum // or Kadane's Algorithm int maxSubArraySum( int a[], int size) { int max_so_far = INT_MIN, max_ending_here = 0; for ( int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } // Function to find the maximum // sum after given operations int maxSum( int a[], int n) { // To store sum of all elements int S = 0; // Maximum sum of a subarray int S1 = maxSubArraySum(a, n); // Calculate the sum of all elements for ( int i = 0; i < n; i++) S += a[i]; return (2 * S1 - S); } // Driver Code int main() { int a[] = { -35, 32, -24, 0, 27, -10, 0, -19 }; // size of an array int n = sizeof (a) / sizeof (a[0]); cout << maxSum(a, n); return 0; } |
Java
// Java program to find the maximum // sum after given operations import java.io.*; class GFG { // Function to calculate Maximum Subarray Sum // or Kadane's Algorithm static int maxSubArraySum( int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0 ; for ( int i = 0 ; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far; } // Function to find the maximum // sum after given operations static int maxSum( int a[], int n) { // To store sum of all elements int S = 0 ; // Maximum sum of a subarray int S1 = maxSubArraySum(a, n); // Calculate the sum of all elements for ( int i = 0 ; i < n; i++) S += a[i]; return ( 2 * S1 - S); } // Driver Code public static void main (String[] args) { int a[] = { - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 }; // size of an array int n = a.length; System.out.println( maxSum(a, n)); } } // This code is contributed by inder_verma |
Python3
# Python3 program to find the maximum # sum after given operations import sys # Function to calculate Maximum # Subarray Sum or Kadane's Algorithm def maxSubArraySum(a, size) : max_so_far = - (sys.maxsize - 1 ) max_ending_here = 0 for i in range (size) : max_ending_here = max_ending_here + a[i] if (max_so_far < max_ending_here) : max_so_far = max_ending_here if (max_ending_here < 0 ) : max_ending_here = 0 return max_so_far # Function to find the maximum # sum after given operations def maxSum(a, n) : # To store sum of all elements S = 0 ; # Maximum sum of a subarray S1 = maxSubArraySum(a, n) # Calculate the sum of all elements for i in range (n) : S + = a[i] return ( 2 * S1 - S) # Driver Code if __name__ = = "__main__" : a = [ - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 ] # size of an array n = len (a) print (maxSum(a, n)) # This code is contributed by Ryuga |
C#
// C# program to find the maximum // sum after given operations using System; class GFG { // Function to calculate Maximum Subarray Sum // or Kadane's Algorithm static int maxSubArraySum( int []a, int size) { int max_so_far = int .MinValue, max_ending_here = 0; for ( int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } // Function to find the maximum // sum after given operations static int maxSum( int []a, int n) { // To store sum of all elements int S = 0; // Maximum sum of a subarray int S1 = maxSubArraySum(a, n); // Calculate the sum of all elements for ( int i = 0; i < n; i++) S += a[i]; return (2 * S1 - S); } // Driver Code public static void Main () { int []a = { -35, 32, -24, 0, 27, -10, 0, -19 }; // size of an array int n = a.Length; Console.WriteLine( maxSum(a, n)); } } // This code is contributed by inder_verma |
PHP
<?php // PHP program to find the maximum // sum after given operations // Function to calculate Maximum Subarray // Sum or Kadane's Algorithm function maxSubArraySum( $a , $size ) { $max_so_far = PHP_INT_MIN; $max_ending_here = 0; for ( $i = 0; $i < $size ; $i ++) { $max_ending_here = $max_ending_here + $a [ $i ]; if ( $max_so_far < $max_ending_here ) $max_so_far = $max_ending_here ; if ( $max_ending_here < 0) $max_ending_here = 0; } return $max_so_far ; } // Function to find the maximum // sum after given operations function maxSum( $a , $n ) { // To store sum of all elements $S = 0; // Maximum sum of a subarray $S1 = maxSubArraySum( $a , $n ); // Calculate the sum of all elements for ( $i = 0; $i < $n ; $i ++) $S += $a [ $i ]; return (2 * $S1 - $S ); } // Driver Code $a = array (-35, 32, -24, 0, 27, -10, 0, -19); // size of an array $n = sizeof( $a ); echo ( maxSum( $a , $n )); // This code is contributed // by Mukul Singh |
99
Recommended Posts:
- Maximum Possible Product in Array after performing given Operations
- Number of array elements derivable from D after performing certain operations
- Number of elements from the array which are reachable after performing given operations on D
- Maximum count of equal numbers in an array after performing given operations
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Find the final sequence of the array after performing given operations
- Minimum element left from the array after performing given operations
- Find the modified array after performing k operations of given type
- Check if at least half array is reducible to zero by performing some operations
- Maximum possible array sum after performing the given operation
- Minimum possible sum of array elements after performing the given operation
- Maximum value in an array after m range increment operations
- Minimum gcd operations to make all array elements one
- Find maximum value of the last element after reducing the array with given operations
- Minimum number of operations on an array to make all elements 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.