Modify array to maximize sum of adjacent differences
Last Updated :
21 Feb, 2023
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++
// C++ program to get maximum consecutive element
// difference sum
#include <bits/stdc++.h>
using namespace std;
// Returns maximum-difference-sum with array
// modifications allowed.
int maximumDifferenceSum(int arr[], int N)
{
// Initialize dp[][] with 0 values.
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++)
{
/* for [i+1][0] (i.e. current modified
value is 1), choose maximum from
dp[i][0] + abs(1 - 1) = dp[i][0] and
dp[i][1] + abs(1 - arr[i]) */
dp[i + 1][0] = max(dp[i][0],
dp[i][1] + abs(1-arr[i]));
/* for [i+1][1] (i.e. current modified value
is arr[i+1]), choose maximum from
dp[i][0] + abs(arr[i+1] - 1) and
dp[i][1] + abs(arr[i+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]);
}
// Driver code to test above methods
int main()
{
int arr[] = {3, 2, 1, 4, 5};
int N = sizeof(arr) / sizeof(arr[0]);
cout << maximumDifferenceSum(arr, N) << endl;
return 0;
}
Java
// java program to get maximum consecutive element
// difference sum
import java.io.*;
class GFG
{
// Returns maximum-difference-sum with array
// modifications allowed.
static int maximumDifferenceSum(int arr[], int N)
{
// Initialize dp[][] with 0 values.
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++)
{
/* for [i+1][0] (i.e. current modified
value is 1), choose maximum from
dp[i][0] + abs(1 - 1) = dp[i][0] and
dp[i][1] + abs(1 - arr[i]) */
dp[i + 1][0] = Math.max(dp[i][0],
dp[i][1] + Math.abs(1 - arr[i]));
/* for [i+1][1] (i.e. current modified value
is arr[i+1]), choose maximum from
dp[i][0] + abs(arr[i+1] - 1) and
dp[i][1] + abs(arr[i+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]);
}
// Driver code
public static void main (String[] args)
{
int arr[] = {3, 2, 1, 4, 5};
int N = arr.length;
System.out.println( maximumDifferenceSum(arr, N));
}
}
// This code is contributed by vt_m
Python3
# Python3 program to get maximum
# consecutive element difference sum
# Returns maximum-difference-sum
# with array modifications allowed.
def maximumDifferenceSum(arr, N):
# Initialize dp[][] with 0 values.
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):
# for [i+1][0] (i.e. current modified
# value is 1), choose maximum from
# dp[i][0] + abs(1 - 1) = dp[i][0]
# and dp[i][1] + abs(1 - arr[i])
dp[i + 1][0] = max(dp[i][0], dp[i][1] +
abs(1 - arr[i]))
# for [i+1][1] (i.e. current modified value
# is arr[i+1]), choose maximum from
# dp[i][0] + abs(arr[i+1] - 1) and
# dp[i][1] + abs(arr[i+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])
# Driver Code
if __name__ == '__main__':
arr = [3, 2, 1, 4, 5]
N = len(arr)
print(maximumDifferenceSum(arr, N))
# This code is contributed by PranchalK
C#
// C# program to get maximum consecutive element
// difference sum
using System;
class GFG {
// Returns maximum-difference-sum with array
// modifications allowed.
static int maximumDifferenceSum(int []arr, int N)
{
// Initialize dp[][] with 0 values.
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++)
{
/* for [i+1][0] (i.e. current modified
value is 1), choose maximum from
dp[i][0] + abs(1 - 1) = dp[i][0] and
dp[i][1] + abs(1 - arr[i]) */
dp[i + 1,0] = Math.Max(dp[i,0],
dp[i,1] + Math.Abs(1 - arr[i]));
/* for [i+1][1] (i.e. current modified value
is arr[i+1]), choose maximum from
dp[i][0] + abs(arr[i+1] - 1) and
dp[i][1] + abs(arr[i+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]);
}
// Driver code
public static void Main ()
{
int []arr = {3, 2, 1, 4, 5};
int N = arr.Length;
Console.Write( maximumDifferenceSum(arr, N));
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program to get maximum
// consecutive element
// difference sum
// Returns maximum-difference-sum
// with array modifications allowed.
function maximumDifferenceSum($arr, $N)
{
// Initialize dp[][]
// with 0 values.
$dp = array(array());
for ($i = 0; $i < $N; $i++)
$dp[$i][0] = $dp[$i][1] = 0;
for ($i = 0; $i < ($N - 1); $i++)
{
/* for [i+1][0] (i.e. current
modified value is 1), choose
maximum from dp[$i][0] +
abs(1 - 1) = dp[i][0] and
dp[$i][1] + abs(1 - arr[i]) */
$dp[$i + 1][0] = max($dp[$i][0],
$dp[$i][1] +
abs(1 - $arr[$i]));
/* for [i+1][1] (i.e. current
modified value is arr[i+1]),
choose maximum from dp[i][0] +
abs(arr[i+1] - 1) and dp[i][1] +
abs(arr[i+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]);
}
// Driver Code
$arr = array(3, 2, 1, 4, 5);
$N = count($arr);
echo maximumDifferenceSum($arr, $N);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to get maximum consecutive element difference sum
// Returns maximum-difference-sum with array
// modifications allowed.
function maximumDifferenceSum(arr, N)
{
// Initialize dp[][] with 0 values.
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++)
{
/* for [i+1][0] (i.e. current modified
value is 1), choose maximum from
dp[i][0] + abs(1 - 1) = dp[i][0] and
dp[i][1] + abs(1 - arr[i]) */
dp[i + 1][0] = Math.max(dp[i][0],
dp[i][1] + Math.abs(1 - arr[i]));
/* for [i+1][1] (i.e. current modified value
is arr[i+1]), choose maximum from
dp[i][0] + abs(arr[i+1] - 1) and
dp[i][1] + abs(arr[i+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));
// This code is contributed by rameshtravel07.
</script>
Time Complexity : O(N)
Auxiliary Space : O(N)
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++
// C++ program for the above approach
#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;
}
// This code is contributed by lokeshpotta20.
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#
// C# program for the above approach
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));
}
}
// This code is contributed by ratiagrawal.
JavaScript
// Javascript program for the above approach
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)
Similar Reads
Maximum sum of difference of adjacent elements
Given a number n. We have to find maximum sum of all permutations of n. The maximum sum will be sum of absolute difference of adjacent elements in array. Examples: Input : 3 Output : 3 Permutations of size 3 are: {1, 2, 3} = 1 + 1 {1, 3, 2} = 2 + 1 {2, 1, 3} = 1 + 2 {2, 3, 1} = 1 + 2 {3, 1, 2} = 2 +
4 min read
Minimize sum of adjacent difference with removal of one element from array
Given an array of positive integers of size greater than 2. The task is to find the minimum value of the sum of consecutive difference modulus of an array, i.e. the value of |A1-A0|+|A2-A1|+|A3-A2|+......+|An-1-An-2|+|An-A(n-1)| after removal of one element from the array, where An represents the nt
8 min read
Maximum possible difference of two subsets of an array
Given an array of n-integers. The array may contain repetitive elements but the highest frequency of any element must not exceed two. You have to make two subsets such that the difference of the sum of their elements is maximum and both of them jointly contain all elements of the given array along w
15+ min read
Minimize sum of squares of adjacent elements difference
Given an Array arr[] or N elements, the task is to minimize the sum of squares of difference of adjacent elements by adding one element at any position of the array. Examples: Input: N = 4, arr = [4, 7, 1, 4]Output: 36Explanation: The sum of squares of difference of adjacent element before inserting
7 min read
Maximize sum by multiplying adjacent difference of Binary String with any digit
Given a binary string S and a string of non-zero digits P of length N, the task is to maximize the sum using the given operations, which can be used as my times you can: You can choose a substring of length 2 or 3.Delete that substring after calculating the absolute difference of adjacent elements f
12 min read
Maximize sum of absolute difference between adjacent elements in Array with sum K
Given two integers N and K, the task is to maximize the sum of absolute differences between adjacent elements of an array of length N and sum K. Examples: Input: N = 5, K = 10 Output: 20 Explanation: The array arr[] with sum 10 can be {0, 5, 0, 5, 0}, maximizing the sum of absolute difference of adj
4 min read
Sum of minimum absolute differences in an array
Given an array of n distinct integers. The task is to find the sum of minimum absolute difference of each array element. For an element arr[i] present at index i in the array, its minimum absolute difference is calculated as: Min absolute difference (arr[i]) = min(abs(arr[i] - arr[j])), where 0 <
7 min read
Find minimum difference with adjacent elements in Array
Given an array A[] of N integers, the task is to find min(A[0], A[1], ..., A[i-1]) - min(A[i+1], A[i+2], ..., A[n-1]) for each i (1 ⤠i ⤠N). Note: If there are no elements at the left or right of i then consider the minimum element towards that part zero. Examples: Input: N = 4, A = {8, 4, 2, 6}Out
12 min read
Minimum sum of absolute difference of pairs of two arrays
Given two arrays a[] and b[] of equal length n. The task is to pair each element of array a to an element in array b, such that sum S of absolute differences of all the pairs is minimum.Suppose, two elements a[i] and a[j] (i != j) of a are paired with elements b[p] and b[q] of b respectively, then p
7 min read
Maximize array sum by alternating the signs of adjacent elements
Given an array, arr[] of size N, the task is to find the maximum possible sum of array elements by alternating the signs of adjacent array elements. Examples: Input: arr[] = { -2, 1, 0 } Output: 3 Explanation: Alternating the signs of (arr[0], arr[1]) modifies arr[] to {2, -1, 0}. Alternating the si
7 min read