Maximum sum obtained by dividing Array into several subarrays as per given conditions
Last Updated :
24 Mar, 2022
Given an array arr[] of size N, the task is to calculate the maximum sum that can be obtained by dividing the array into several subarrays(possibly one), where each subarray starting at index i and ending at index j (j>=i) contributes arr[j]-arr[i] to the sum.
Examples:
Input: arr[]= {1, 5, 3}, N=3
Output:
4
Explanation: The array can be divided into 2 subarrays:
- {1, 5} -> sum contributed by the subarray = 5-1 = 4
- {3} -> sum contributed by the subarray = 3-3 = 0
Therefore, the answer is 4.(It can be shown that there is no other way of dividing this array in multiple subarrays such that the answer is greater than 4).
Input: arr[] = {6, 2, 1}, N=3
Output:
0
Naive Approach: The naive approach is to consider all possible ways of dividing arr into 1 or more subarrays and calculating the maximum sum obtained for each.
Time Complexity: O(N*2N)
Auxiliary Space: O(1)
Observation: The observations necessary to solve the problem are below:
- The array should be divided into several(possibly one) subarrays such that each subarray is the longest increasing subarray. For example, if arr[]={3,5,7,9,1}, it is optimal to consider {3,5,7,9} as a subarray as it would contribute 9-3=6 to the sum. Breaking it up further decreases the sum which is not optimal.
- Every element of a non-increasing subarray should be considered as single element subarrays so that they contribute 0 to the sum. Otherwise, they would be contributing a negative value. For example, if arr[i]>arr[i+1], it is optimal to consider two subarrays of length 1 containing arr[i] and arr[i+1] separately, so that they contribute (arr[i]-arr[i]) +(arr[i+1]-arr[i+1]) =0 to the answer. If they were considered together, they would contribute arr[i+1]-arr[i] which is a negative number, thus decreasing the sum.
Efficient Approach: Follow the steps below to solve the problem:
- Initialize a variable Sum to 0.
- Traverse arr from 1 to N-1, using the variable i, and do the following:
- If arr[i]>arr[i-1], add arr[i]-arr[i-1] to Sum. This works because the sum of differences of adjacent elements in a sorted array is equal to the difference of the elements at extreme ends. Here, only the increasing subarrays are considered as arr[i]>arr[i-1].
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the required answer
int maximumSum(int arr[], int N)
{
// Stores maximum sum
int Sum = 0;
for (int i = 1; i < N; i++) {
// Adding the difference of elements at ends of
// increasing subarray to the answer
if (arr[i] > arr[i - 1])
Sum += (arr[i] - arr[i - 1]);
}
return Sum;
}
// Driver Code
int main()
{
// Input
int arr[] = { 1, 5, 3 };
int N = (sizeof(arr) / (sizeof(arr[0])));
// Function calling
cout << maximumSum(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the required answer
public static int maximumSum(int arr[], int N)
{
// Stores maximum sum
int Sum = 0;
for(int i = 1; i < N; i++)
{
// Adding the difference of elements at ends
// of increasing subarray to the answer
if (arr[i] > arr[i - 1])
Sum += (arr[i] - arr[i - 1]);
}
return Sum;
}
// Driver Code
public static void main(String[] args)
{
// Input
int arr[] = { 1, 5, 3 };
int N = arr.length;
// Function calling
System.out.println(maximumSum(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function to find the required answer
def maximumSum(arr, N):
# Stores maximum sum
Sum = 0;
for i in range(1,N):
# Adding the difference of elements at ends of
# increasing subarray to the answer
if (arr[i] > arr[i - 1]):
Sum += (arr[i] - arr[i - 1])
return Sum;
# Driver Code
#Input
arr = [ 1, 5, 3 ];
N = len(arr)
# Function calling
print(maximumSum(arr, N));
# This code is contributed by SoumikMondal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the required answer
static int maximumSum(int []arr, int N)
{
// Stores maximum sum
int Sum = 0;
for(int i = 1; i < N; i++)
{
// Adding the difference of elements at
// ends of increasing subarray to the answer
if (arr[i] > arr[i - 1])
Sum += (arr[i] - arr[i - 1]);
}
return Sum;
}
// Driver Code
public static void Main()
{
// Input
int []arr = { 1, 5, 3 };
int N = arr.Length;
// Function calling
Console.Write(maximumSum(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the required answer
function maximumSum(arr, N)
{
// Stores maximum sum
let Sum = 0;
for(let i = 1; i < N; i++)
{
// Adding the difference of elements at ends
// of increasing subarray to the answer
if (arr[i] > arr[i - 1])
Sum += (arr[i] - arr[i - 1]);
}
return Sum;
}
// Driver Code
// Input
let arr = [ 1, 5, 3 ];
let N = arr.length;
// Function calling
document.write(maximumSum(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximum length of same indexed subarrays from two given arrays satisfying the given condition Given two arrays arr[] and brr[] and an integer C, the task is to find the maximum possible length, say K, of the same indexed subarrays such that the sum of the maximum element in the K-length subarray in brr[] with the product between K and sum of the K-length subarray in arr[] does not exceed C.
15+ min read
Maximum sum of K-length subarray consisting of same number of distinct elements as the given array Given an array arr[] consisting of N integers and an integer K, the task is to find a subarray of size K with maximum sum and count of distinct elements same as that of the original array. Examples: Input: arr[] = {7, 7, 2, 4, 2, 7, 4, 6, 6, 6}, K = 6Output: 31Explanation: The given array consists o
15+ min read
Find maximum sum by replacing the Subarray in given range Given an array arr[], the task is to find the maximum sum possible such that any subarray of the indices from [l, r] i.e all subarray elements from arr[l] to arr[r] can be replaced with |arr[l] - arr[r]| any number of times. Examples: Input: arr[] = { 9, 1}Output: 16Explanation: The subarray [l, r]
9 min read
Divide an array into K subarray with the given condition Given an array arr[] and an integer K. The task is to divide the array into K parts ( subarray ) such that the sum of the values of all subarray is minimum.The value of every subarray is defined as: Take the maximum from that subarray.Subtract each element of the subarray with the maximum.Take the s
9 min read
Find the maximum sum after dividing array A into M Subarrays Given a sorted array A[] of size N and an integer M. You need to divide the array A[] into M non-empty consecutive subarray (1 ? M ? N) of any size such that each element is present in exactly one of the M-subarray. After dividing the array A[] into M subarrays you need to calculate the sum [max(i)
10 min read