Maximize sum of consecutive differences in a circular array
Last Updated :
08 Jul, 2022
Given an array of n elements. Consider array as circular array i.e element after an is a1. The task is to find maximum sum of the difference between consecutive elements with rearrangement of array element allowed i.e after rearrangement of element find |a1 - a2| + |a2 - a3| + ...... + |an - 1 - an| + |an - a1|.
Examples:
Input : arr[] = { 4, 2, 1, 8 }
Output : 18
Rearrange given array as : { 1, 8, 2, 4 }
Sum of difference between consecutive element
= |1 - 8| + |8 - 2| + |2 - 4| + |4 - 1|
= 7 + 6 + 2 + 3
= 18.
Input : arr[] = { 10, 12, 15 }
Output : 10
The idea is to use Greedy Approach and try to bring elements having greater difference closer.
Consider the sorted permutation of the given array a1, a1, a2,...., an - 1, an such that a1 < a2 < a3.... < an - 1 < an.
Now, to obtain the answer having maximum sum of difference between consecutive element, arrange element in following manner:
a1, an, a2, an-1,...., an/2, a(n/2) + 1
We can observe that the arrangement produces the optimal answer, as all a1, a2, a3,....., a(n/2)-1, an/2 are subtracted twice while a(n/2)+1 , a(n/2)+2, a(n/2)+3,....., an - 1, an are added twice.
Note: a(n/2)+1 This term is considered only for even n because for odd n, it is added once and subtracted once and hence cancels out.
Implementation:
C++
// C++ program to maximize the sum of difference
// between consecutive elements in circular array
#include <bits/stdc++.h>
using namespace std;
// Return the maximum Sum of difference between
// consecutive elements.
int maxSum(int arr[], int n)
{
int sum = 0;
// Sorting the array.
sort(arr, arr + n);
// Subtracting a1, a2, a3,....., a(n/2)-1, an/2
// twice and adding a(n/2)+1, a(n/2)+2, a(n/2)+3,.
// ...., an - 1, an twice.
for (int i = 0; i < n/2; i++)
{
sum -= (2 * arr[i]);
sum += (2 * arr[n - i - 1]);
}
return sum;
}
// Driver Program
int main()
{
int arr[] = { 4, 2, 1, 8 };
int n = sizeof(arr)/sizeof(arr[0]);
cout << maxSum(arr, n) << endl;
return 0;
}
Java
// Java program to maximize the sum of difference
// between consecutive elements in circular array
import java.io.*;
import java.util.Arrays;
class MaxSum
{
// Return the maximum Sum of difference between
// consecutive elements.
static int maxSum(int arr[], int n)
{
int sum = 0;
// Sorting the array.
Arrays.sort(arr);
// Subtracting a1, a2, a3,....., a(n/2)-1,
// an/2 twice and adding a(n/2)+1, a(n/2)+2,
// a(n/2)+3,....., an - 1, an twice.
for (int i = 0; i < n/2; i++)
{
sum -= (2 * arr[i]);
sum += (2 * arr[n - i - 1]);
}
return sum;
}
// Driver Program
public static void main (String[] args)
{
int arr[] = { 4, 2, 1, 8 };
int n = arr.length;
System.out.println(maxSum(arr, n));
}
}
/*This code is contributed by Prakriti Gupta*/
Python3
# Python3 program to maximize the sum of difference
# between consecutive elements in circular array
# Return the maximum Sum of difference
# between consecutive elements
def maxSum(arr, n):
sum = 0
# Sorting the array
arr.sort()
# Subtracting a1, a2, a3,....., a(n/2)-1, an/2
# twice and adding a(n/2)+1, a(n/2)+2, a(n/2)+3,.
# ...., an - 1, an twice.
for i in range(0, int(n / 2)) :
sum -= (2 * arr[i])
sum += (2 * arr[n - i - 1])
return sum
# Driver Program
arr = [4, 2, 1, 8]
n = len(arr)
print (maxSum(arr, n))
# This code is contributed by Shreyanshi Arun.
C#
// C# program to maximize the sum of difference
// between consecutive elements in circular array
using System;
class MaxSum {
// Return the maximum Sum of difference
// between consecutive elements.
static int maxSum(int[] arr, int n)
{
int sum = 0;
// Sorting the array.
Array.Sort(arr);
// Subtracting a1, a2, a3, ....., a(n/2)-1,
// an/2 twice and adding a(n/2)+1, a(n/2)+2,
// a(n/2)+3, ....., an - 1, an twice.
for (int i = 0; i < n / 2; i++) {
sum -= (2 * arr[i]);
sum += (2 * arr[n - i - 1]);
}
return sum;
}
// Driver Program
public static void Main()
{
int[] arr = { 4, 2, 1, 8 };
int n = arr.Length;
Console.WriteLine(maxSum(arr, n));
}
}
//This Code is contributed by vt_m.
JavaScript
<script>
// JavaScript program for the above approach
// Return the maximum Sum of difference between
// consecutive elements.
function maxSum(arr, n)
{
let sum = 0;
// Sorting the array.
arr.sort();
// Subtracting a1, a2, a3,....., a(n/2)-1,
// an/2 twice and adding a(n/2)+1, a(n/2)+2,
// a(n/2)+3,....., an - 1, an twice.
for (let i = 0; i < n/2; i++)
{
sum -= (2 * arr[i]);
sum += (2 * arr[n - i - 1]);
}
return sum;
}
// Driver Code
let arr = [ 4, 2, 1, 8 ];
let n = arr.length;
document.write(maxSum(arr, n));
// This code is contributed by chinmoy1997pal.
</script>
Time Complexity: O(nlogn).
Auxiliary Space : O(1)
Similar Reads
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Maximise the size of consecutive element subsets in an array Given an integer array and an integer k. The array elements denote positions of points on a 1-D number line, find the maximum size of the subset of points that can have consecutive values of points which can be formed by placing another k points on the number line. Note that all coordinates should b
12 min read
Modify array to maximize sum of adjacent differences 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 mod
9 min read
Minimize the maximum absolute difference of adjacent elements in a circular array Given a circular array arr of N integers, the task is to minimize the maximum absolute difference of adjacent elements of the array without any removals. Examples: Input: arr[] = {1, 3, 10, 2, 0, 9, 6} Output: {0, 2, 6, 10, 9, 3, 1} Explanation: In the above example, the maximum difference between a
6 min read
Maximum difference between two elements in an Array Given an array arr[] of N integers, the task is to find the maximum difference between any two elements of the array.Examples: Input: arr[] = {2, 1, 5, 3} Output: 4 |5 - 1| = 4 Input: arr[] = {-10, 4, -9, -5} Output: 14 Naive Approach:- As the maximum difference will be in between smallest and the l
9 min read