Given an array arr[] consisting of N integers, the task is to split the array into subarrays such that the sum of the difference between the maximum and minimum elements for all the subarrays is maximum.
Examples :
Input: arr[] = {8, 1, 7, 9, 2}
Output: 14
Explanation:
Consider splitting the given array into subarrays as {8, 1} and {7, 9, 2}. Now, the difference between maximum and minimum elements are:
- {8, 1}: Difference is (8 – 1) = 7.
- {7, 9, 2}: Difference is (9 – 2) = 7.
Therefore, the sum of the difference is 7 + 7 = 14.
Input: arr[] = {1, 2, 1, 0, 5}
Output: 6
Approach: The given problem can be solved by using Dynamic Programming. Follow the steps to solve the problem:
- Initialize an array, say dp[], where dp[i] represents the maximum sum of the difference between the maximum and minimum element for all the subarray for the first i array element.
- Initialize dp[0] as 0.
- Traverse the given array over the range [1, N – 1] and perform the following steps:
- Initialize a variable, say min as arr[i], that stores the minimum element over the range [0, i].
- Initialize a variable, say max as arr[i], that stores the maximum element over the range [0, i].
- Iterate over the range [0, i] using the variable j in the reverse order and perform the following steps:
- Update the value of min as the minimum of min and arr[j].
- Update the value of max as the minimum of max and arr[j].
- Update the value of dp[j] to the maximum of dp[j] and (max – min + dp[i]).
- After completing the above steps, print the value of dp[N – 1] as the result.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int getValue( int arr[], int N)
{
int dp[N];
memset (dp, 0, sizeof (dp));
dp[0] = 0;
for ( int i = 1; i < N; i++)
{
int minn = arr[i];
int maxx = arr[i];
for ( int j = i; j >= 0; j--)
{
minn = min(arr[j], minn);
maxx = max(arr[j], maxx);
dp[i] = max(dp[i], maxx - minn +
((j >= 1) ? dp[j - 1] : 0));
}
}
return dp[N - 1];
}
int main()
{
int arr[] = { 8, 1, 7, 9, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getValue(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int getValue( int [] arr, int N)
{
int dp[] = new int [N];
dp[ 0 ] = 0 ;
for ( int i = 1 ; i < N; i++) {
int min = arr[i];
int max = arr[i];
for ( int j = i; j >= 0 ; j--) {
min = Math.min(arr[j], min);
max = Math.max(arr[j], max);
dp[i] = Math.max(
dp[i],
max - min + ((j >= 1 )
? dp[j - 1 ]
: 0 ));
}
}
return dp[N - 1 ];
}
public static void main(String args[])
{
int arr[] = { 8 , 1 , 7 , 9 , 2 };
int N = arr.length;
System.out.println(getValue(arr, N));
}
}
|
C#
using System;
class GFG{
static int getValue( int [] arr, int N)
{
int [] dp = new int [N];
dp[0] = 0;
for ( int i = 1; i < N; i++)
{
int min = arr[i];
int max = arr[i];
for ( int j = i; j >= 0; j--)
{
min = Math.Min(arr[j], min);
max = Math.Max(arr[j], max);
dp[i] = Math.Max(
dp[i],
max - min + ((j >= 1) ?
dp[j - 1] : 0));
}
}
return dp[N - 1];
}
static public void Main()
{
int [] arr = { 8, 1, 7, 9, 2 };
int N = arr.Length;
Console.Write(getValue(arr, N));
}
}
|
Python3
def getValue(arr, N):
dp = [ 0 for i in range (N)]
for i in range ( 1 , N):
minn = arr[i]
maxx = arr[i]
j = i
while (j > = 0 ):
minn = min (arr[j], minn)
maxx = max (arr[j], maxx)
dp[i] = max (dp[i], maxx - minn + (dp[j - 1 ] if (j > = 1 ) else 0 ))
j - = 1
return dp[N - 1 ]
if __name__ = = '__main__' :
arr = [ 8 , 1 , 7 , 9 , 2 ]
N = len (arr)
print (getValue(arr, N))
|
Javascript
<script>
function getValue(arr, N)
{
let dp = Array.from({length: N}, (_, i) => 0);
dp[0] = 0;
for (let i = 1; i < N; i++) {
let min = arr[i];
let max = arr[i];
for (let j = i; j >= 0; j--) {
min = Math.min(arr[j], min);
max = Math.max(arr[j], max);
dp[i] = Math.max(
dp[i],
max - min + ((j >= 1)
? dp[j - 1]
: 0));
}
}
return dp[N - 1];
}
let arr = [ 8, 1, 7, 9, 2 ];
let N = arr.length;
document.write(getValue(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)