Given an array, we need to modify the values of this array in such a way that the sum of absolute differences between two consecutive elements is maximized. If the value of an array element is X, then we can change it to either 1 or X.
Examples :
Input : arr[] = [3, 2, 1, 4, 5]
Output : 8
We can modify above array as,
Modified arr[] = [3, 1, 1, 4, 1]
Sum of differences =
|1-3| + |1-1| + |4-1| + |1-4| = 8
Which is the maximum obtainable value
among all choices of modification.
Input : arr[] = [1, 8, 9]
Output : 14
Method 1: This problem is a variation of Assembly Line Scheduling and can be solved using dynamic programming. We need to maximize sum of differences each value X should be changed to either 1 or X. To achieve above stated condition we take a dp array of array length size with 2 columns, where dp[i][0] stores the maximum value of sum using first i elements only if ith array value is modified to 1 and dp[i][1] stores the maximum value of sum using first i elements if ith array value is kept as a[i] itself.Main thing to observe is,
C++
#include <bits/stdc++.h>
using namespace std;
int maximumDifferenceSum( int arr[], int N)
{
int dp[N][2];
for ( int i = 0; i < N; i++)
dp[i][0] = dp[i][1] = 0;
for ( int i=0; i<(N-1); i++)
{
dp[i + 1][0] = max(dp[i][0],
dp[i][1] + abs (1-arr[i]));
dp[i + 1][1] = max(dp[i][0] + abs (arr[i+1] - 1),
dp[i][1] + abs (arr[i+1] - arr[i]));
}
return max(dp[N-1][0], dp[N-1][1]);
}
int main()
{
int arr[] = {3, 2, 1, 4, 5};
int N = sizeof (arr) / sizeof (arr[0]);
cout << maximumDifferenceSum(arr, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int maximumDifferenceSum( int arr[], int N)
{
int dp[][] = new int [N][ 2 ];
for ( int i = 0 ; i < N; i++)
dp[i][ 0 ] = dp[i][ 1 ] = 0 ;
for ( int i = 0 ; i< (N - 1 ); i++)
{
dp[i + 1 ][ 0 ] = Math.max(dp[i][ 0 ],
dp[i][ 1 ] + Math.abs( 1 - arr[i]));
dp[i + 1 ][ 1 ] = Math.max(dp[i][ 0 ] +
Math.abs(arr[i + 1 ] - 1 ),
dp[i][ 1 ] + Math.abs(arr[i + 1 ]
- arr[i]));
}
return Math.max(dp[N - 1 ][ 0 ], dp[N - 1 ][ 1 ]);
}
public static void main (String[] args)
{
int arr[] = { 3 , 2 , 1 , 4 , 5 };
int N = arr.length;
System.out.println( maximumDifferenceSum(arr, N));
}
}
|
Python3
def maximumDifferenceSum(arr, N):
dp = [[ 0 , 0 ] for i in range (N)]
for i in range (N):
dp[i][ 0 ] = dp[i][ 1 ] = 0
for i in range (N - 1 ):
dp[i + 1 ][ 0 ] = max (dp[i][ 0 ], dp[i][ 1 ] +
abs ( 1 - arr[i]))
dp[i + 1 ][ 1 ] = max (dp[i][ 0 ] + abs (arr[i + 1 ] - 1 ),
dp[i][ 1 ] + abs (arr[i + 1 ] - arr[i]))
return max (dp[N - 1 ][ 0 ], dp[N - 1 ][ 1 ])
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 , 4 , 5 ]
N = len (arr)
print (maximumDifferenceSum(arr, N))
|
C#
using System;
class GFG {
static int maximumDifferenceSum( int []arr, int N)
{
int [,]dp = new int [N,2];
for ( int i = 0; i < N; i++)
dp[i,0] = dp[i,1] = 0;
for ( int i = 0; i < (N - 1); i++)
{
dp[i + 1,0] = Math.Max(dp[i,0],
dp[i,1] + Math.Abs(1 - arr[i]));
dp[i + 1,1] = Math.Max(dp[i,0] +
Math.Abs(arr[i + 1] - 1),
dp[i,1] + Math.Abs(arr[i + 1]
- arr[i]));
}
return Math.Max(dp[N - 1,0], dp[N - 1,1]);
}
public static void Main ()
{
int []arr = {3, 2, 1, 4, 5};
int N = arr.Length;
Console.Write( maximumDifferenceSum(arr, N));
}
}
|
PHP
<?php
function maximumDifferenceSum( $arr , $N )
{
$dp = array ( array ());
for ( $i = 0; $i < $N ; $i ++)
$dp [ $i ][0] = $dp [ $i ][1] = 0;
for ( $i = 0; $i < ( $N - 1); $i ++)
{
$dp [ $i + 1][0] = max( $dp [ $i ][0],
$dp [ $i ][1] +
abs (1 - $arr [ $i ]));
$dp [ $i + 1][1] = max( $dp [ $i ][0] +
abs ( $arr [ $i + 1] - 1),
$dp [ $i ][1] +
abs ( $arr [ $i + 1] -
$arr [ $i ]));
}
return max( $dp [ $N - 1][0], $dp [ $N - 1][1]);
}
$arr = array (3, 2, 1, 4, 5);
$N = count ( $arr );
echo maximumDifferenceSum( $arr , $N );
?>
|
Javascript
<script>
function maximumDifferenceSum(arr, N)
{
let dp = new Array(N);
for (let i = 0; i < N; i++)
{
dp[i] = new Array(2);
for (let j = 0; j < 2; j++)
{
dp[i][j] = 0;
}
}
for (let i = 0; i < N; i++)
dp[i][0] = dp[i][1] = 0;
for (let i = 0; i< (N - 1); i++)
{
dp[i + 1][0] = Math.max(dp[i][0],
dp[i][1] + Math.abs(1 - arr[i]));
dp[i + 1][1] = Math.max(dp[i][0] +
Math.abs(arr[i + 1] - 1),
dp[i][1] + Math.abs(arr[i + 1]
- arr[i]));
}
return Math.max(dp[N - 1][0], dp[N - 1][1]);
}
let arr = [3, 2, 1, 4, 5];
let N = arr.length;
document.write( maximumDifferenceSum(arr, N));
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(N)
This article is contributed by Utkarsh Trivedi.
Method 2:
For calculating answers at any moment, only previous state values are needed. So instead of using the entire DP array, we can reduce space complexity by using just two variables prev_change and prev_nochange.
C++
#include <iostream>
using namespace std;
int maximumDifferenceSum( int arr[], int n) {
int prev_change = 0, prev_nochange = 0;
for ( int i = 1; i < n; i++) {
int change = max(prev_change, arr[i - 1] - 1 + prev_nochange);
int nochange = max(prev_nochange + abs (arr[i] - arr[i - 1]), arr[i] - 1 + prev_change);
prev_change = change;
prev_nochange = nochange;
}
return max(prev_change, prev_nochange);
}
int main() {
int arr[] = {3, 2, 1, 4, 5};
int N = sizeof (arr) / sizeof (arr[0]);
cout << maximumDifferenceSum(arr, N) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int maximumDifferenceSum( int arr[], int n)
{
int prev_change = 0 , prev_nochange = 0 ;
for ( int i = 1 ; i < n; i++)
{
int change = Math.max(prev_change, arr[i - 1 ] - 1 + prev_nochange);
int nochange = Math.max(prev_nochange + Math.abs(arr[i] - arr[i - 1 ]),
arr[i] - 1 + prev_change);
prev_change = change;
prev_nochange = nochange;
}
return Math.max(prev_change, prev_nochange);
}
public static void main(String args[])
{
int arr[] = { 3 , 2 , 1 , 4 , 5 };
int N = arr.length;
System.out.println(maximumDifferenceSum(arr, N));
}
}
|
Python3
def maximumDifferenceSum(arr, n):
prev_change, prev_nochange = 0 , 0
for i in range ( 1 ,n):
change = max (prev_change , arr[i - 1 ] - 1 + prev_nochange)
nochange = max (prev_nochange + abs (arr[i] - arr[i - 1 ]) , arr[i] - 1 + prev_change)
prev_change = change
prev_nochange = nochange
return max (prev_change, prev_nochange)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 , 4 , 5 ]
N = len (arr)
print (maximumDifferenceSum(arr, N))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static int maximumDifferenceSum( int [] arr, int n)
{
int prev_change = 0, prev_nochange = 0;
for ( int i = 1; i < n; i++) {
int change = Math.Max(prev_change, arr[i - 1] - 1 + prev_nochange);
int nochange = Math.Max(prev_nochange + Math.Abs(arr[i] - arr[i - 1]), arr[i] - 1 + prev_change);
prev_change = change;
prev_nochange = nochange;
}
return Math.Max(prev_change, prev_nochange);
}
static public void Main()
{
int [] arr = {3, 2, 1, 4, 5};
int N = arr.Length;
Console.Write(maximumDifferenceSum(arr, N));
}
}
|
Javascript
function maximumDifferenceSum( arr, n) {
let prev_change = 0, prev_nochange = 0;
for (let i = 1; i < n; i++) {
let change = Math.max(prev_change, arr[i - 1] - 1 + prev_nochange);
let nochange = Math.max(prev_nochange + Math.abs(arr[i] -
arr[i - 1]), arr[i] - 1 + prev_change);
prev_change = change;
prev_nochange = nochange;
}
return Math.max(prev_change, prev_nochange);
}
let arr = [3, 2, 1, 4, 5];
let N = arr.length;
console.log(maximumDifferenceSum(arr, N));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!