Maximize sum by choosing a subsegment from array [l, r] and convert arr[i] to (M–arr[i]) at most once
Given an array, arr[] consisting of N positive integers and a positive integer M, the task is to maximize the sum of the array after performing at most one operation. In one operation, choose a subsegment from the array [l, r] and convert arr[i] to M – arr[i] where l?i?r.
Examples:
Input: arr[] = {2, 4, 3}, M = 5
Output: 10
Explanation: Replace numbers in the subarray from index 0 to index 0, so the new array becomes [5-2, 4, 3], so the maximum sum will be (5-2) + 4 + 3 = 10
Input: arr[] = {4, 3, 4}, M = 5
Output: 11
Naive Approach: The simplest approach to solve the problem is to apply the operation on all the subarrays of the array and find the maximum sum applying the given operation.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized further by using Kadane’s Algorithm. Follow the steps below to solve the problem:
- Initialize a variable, say sum as 0 to store the sum of the array.
- Iterate in the range [0, N-1] using the variable i and add arr[i] to the variable sum and modify the value of arr[i] as M – 2×arr[i].
- Now apply Kadane’s algorithm to the array arr[].
- Initialize two variables, say mx and ans as 0.
- Iterate in the range [0, N-1] using the variable i and perform the following steps:
- Add arr[i] to the ans.
- If ans<0, then modify the value of ans as 0.
- Modify the value of mx as max(mx, ans).
- Print the value of sum + mx as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxSum( int arr[], int n, int M)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
int ans = 0, mx = 0;
for ( int i = 0; i < n; i++) {
ans += arr[i];
if (ans < 0)
ans = 0;
mx = max(mx, ans);
}
return mx + sum;
}
int main()
{
int arr[] = { 2, 4, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = 5;
cout << MaxSum(arr, N, M);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MaxSum( int arr[], int n, int M)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
int ans = 0 , mx = 0 ;
for ( int i = 0 ; i < n; i++)
{
ans += arr[i];
if (ans < 0 )
ans = 0 ;
mx = Math.max(mx, ans);
}
return mx + sum;
}
public static void main (String[] args) {
int arr[] = { 2 , 4 , 3 };
int N = arr.length;
int M = 5 ;
System.out.println(MaxSum(arr, N, M));
}
}
|
Python3
def MaxSum(arr, n, M):
sum = 0
for i in range (n):
sum + = arr[i]
arr[i] = (M - 2 * arr[i])
ans = 0
mx = 0
for i in range (n):
ans + = arr[i]
if (ans < 0 ):
ans = 0
mx = max (mx, ans)
return mx + sum
if __name__ = = '__main__' :
arr = [ 2 , 4 , 3 ]
N = len (arr)
M = 5
print (MaxSum(arr, N, M))
|
C#
using System;
class GFG {
static int MaxSum( int [] arr, int n, int M)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
int ans = 0, mx = 0;
for ( int i = 0; i < n; i++) {
ans += arr[i];
if (ans < 0)
ans = 0;
mx = Math.Max(mx, ans);
}
return mx + sum;
}
public static void Main()
{
int [] arr = { 2, 4, 3 };
int N = arr.Length;
int M = 5;
Console.WriteLine(MaxSum(arr, N, M));
}
}
|
Javascript
<script>
function MaxSum(arr, n, M) {
let sum = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
let ans = 0, mx = 0;
for (let i = 0; i < n; i++) {
ans += arr[i];
if (ans < 0)
ans = 0;
mx = Math.max(mx, ans);
}
return mx + sum;
}
let arr = [2, 4, 3];
let N = arr.length;
let M = 5;
document.write(MaxSum(arr, N, M));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
06 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...