Maximize sum by choosing a subsegment from array [l, r] and convert arr[i] to (M–arr[i]) at most once
Last Updated :
06 Jul, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to maximize the sum after
// performing atmost one operation
int MaxSum(int arr[], int n, int M)
{
// Variable to store the sum
int sum = 0;
// Traverse the array and modify arr[]
for (int i = 0; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
int ans = 0, mx = 0;
// Apply Kadane's algorithm
for (int i = 0; i < n; i++) {
// Add a[i] to ans
ans += arr[i];
// If ans<0, modify the value
// of ans as 0
if (ans < 0)
ans = 0;
// Update the value of mx
mx = max(mx, ans);
}
// Return the maximum sum
return mx + sum;
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 2, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int M = 5;
// Function Call
cout << MaxSum(arr, N, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to maximize the sum after
// performing atmost one operation
static int MaxSum(int arr[], int n, int M)
{
// Variable to store the sum
int sum = 0;
// Traverse the array and modify arr[]
for (int i = 0; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
int ans = 0, mx = 0;
// Apply Kadane's algorithm
for (int i = 0; i < n; i++)
{
// Add a[i] to ans
ans += arr[i];
// If ans<0, modify the value
// of ans as 0
if (ans < 0)
ans = 0;
// Update the value of mx
mx = Math.max(mx, ans);
}
// Return the maximum sum
return mx + sum;
}
// Driver Code
public static void main (String[] args) {
// Given Input
int arr[] = { 2, 4, 3 };
int N = arr.length;
int M = 5;
// Function Call
System.out.println(MaxSum(arr, N, M));
}
}
// This code is contributed by potta lokesh.
Python3
# python 3 program for the above approach
# Function to maximize the sum after
# performing atmost one operation
def MaxSum(arr, n, M):
# Variable to store the sum
sum = 0
# Traverse the array and modify arr[]
for i in range(n):
sum += arr[i]
arr[i] = (M - 2 * arr[i])
ans = 0
mx = 0
# Apply Kadane's algorithm
for i in range(n):
# Add a[i] to ans
ans += arr[i]
# If ans<0, modify the value
# of ans as 0
if (ans < 0):
ans = 0
# Update the value of mx
mx = max(mx, ans)
# Return the maximum sum
return mx + sum
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [2, 4, 3]
N = len(arr)
M = 5
# Function Call
print(MaxSum(arr, N, M))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG {
// Function to maximize the sum after
// performing atmost one operation
static int MaxSum(int[] arr, int n, int M)
{
// Variable to store the sum
int sum = 0;
// Traverse the array and modify arr[]
for (int i = 0; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
int ans = 0, mx = 0;
// Apply Kadane's algorithm
for (int i = 0; i < n; i++) {
// Add a[i] to ans
ans += arr[i];
// If ans<0, modify the value
// of ans as 0
if (ans < 0)
ans = 0;
// Update the value of mx
mx = Math.Max(mx, ans);
}
// Return the maximum sum
return mx + sum;
}
// Driver Code
public static void Main()
{
// Given Input
int[] arr = { 2, 4, 3 };
int N = arr.Length;
int M = 5;
// Function Call
Console.WriteLine(MaxSum(arr, N, M));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript program for the above approach
// Function to maximize the sum after
// performing atmost one operation
function MaxSum(arr, n, M) {
// Variable to store the sum
let sum = 0;
// Traverse the array and modify arr[]
for (let i = 0; i < n; i++) {
sum += arr[i];
arr[i] = (M - 2 * arr[i]);
}
let ans = 0, mx = 0;
// Apply Kadane's algorithm
for (let i = 0; i < n; i++) {
// Add a[i] to ans
ans += arr[i];
// If ans<0, modify the value
// of ans as 0
if (ans < 0)
ans = 0;
// Update the value of mx
mx = Math.max(mx, ans);
}
// Return the maximum sum
return mx + sum;
}
// Driver Code
// Given Input
let arr = [2, 4, 3];
let N = arr.length;
let M = 5;
// Function Call
document.write(MaxSum(arr, N, M));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize median of sequence formed by choosing at least one from adjacent pairs Given an array Arr[ ] of N integers where 1 ⤠N ⤠105. The task is to choose any number of elements from the array such that: For each i ( 0⤠i ⥠N-2 ), at least one of the ith and (i+1)th elements must be chosen.Find the maximum possible median of integers from the subsequence of the array thus for
13 min read
Maximize array sum by replacing at most L elements to R for Q queries Given an array arr[] consisting of N integers and an array Query[][] consisting of M pairs of the type {L, R}, the task is to find the maximum sum of the array by performing the queries Query[][] such that for each query {L, R} replace at most L array elements to the value R. Examples: Input: arr[]=
7 min read
Maximize sum of chosen Array elements with value at most M Given an array arr[] of N positive numbers and an integer M. The task is to maximize the value of M by adding array elements when arr[i] ⤠M. Note: Any array element can be added at most once. Examples: Input: arr[] = {3, 9, 19, 5, 21}, M = 10Output: 67Explanation: One way to getthe value isM > 3
4 min read
Maximum range subarray for each index in Array such that A[i] = min(A[L], A[L+1], ⦠A[R]) Given an array arr[] of N distinct integers, the task is to calculate for each index i (1â¤iâ¤N) a range [L, R] such that arr[i] = min(arr[L], arr[L+1], ⦠arr[R]), where Lâ¤iâ¤R and R-L is maximized. Examples: Input: N = 3, arr[] = {1, 3, 2}Output: 1 32 22 3Explanation: 1 is minimum in the range [1, 3]3
11 min read
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 Given a positive integer N, the task is to construct an array of length N and find the maximum value at index K such that the sum of all the array elements is at most M and the absolute difference between any two consecutive array elements is at most 1.Examples:Input: N = 3, M = 7, K = 1Output: 3Exp
6 min read