给定一个包含n 个元素的数组。将数组视为圆形数组,即n之后的元素是1 。任务是找到允许重新排列数组元素的连续元素之间差异的最大和,即在重新排列元素之后 find |a 1 – a 2 | + |a 2 – a 3 | + …… + |a n – 1 – a n | + |a n – a 1 |。
例子:
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
这个想法是使用贪婪方法并尝试将具有更大差异的元素拉近。
考虑给定数组 a 1 , a 1 , a 2 ,…., a n – 1 , a n的排序排列使得 a 1 < a 2 < a 3 …。 < a n – 1 < a n 。
现在,要获得连续元素之间差值之和最大的答案,请按以下方式排列元素:
a 1 , a n , a 2 , a n-1 ,…., a n/2 , a (n/2) + 1
我们可以观察到这种安排产生了最佳答案,因为所有 a 1 , a 2 , a 3 ,….., a (n/2)-1 , a n/2被减去两次而 a (n/2)+ 1 , a (n/2)+2 , a (n/2)+3 ,….., a n – 1 , a n相加两次。
注 – a (n/2)+1该项仅考虑偶数 n,因为对于奇数 n,它被添加一次并减去一次,因此被抵消。
C++
// C++ program to maximize the sum of difference
// between consecutive elements in circular array
#include
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
输出 :
18
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。