给定一个数组,我们需要找到给定数组的任何排列的绝对差的最大和。
例子:
Input : { 1, 2, 4, 8 }
Output : 18
Explanation : For the given array there are
several sequence possible
like : {2, 1, 4, 8}
{4, 2, 1, 8} and some more.
Now, the absolute difference of an array sequence will be
like for this array sequence {1, 2, 4, 8}, the absolute
difference sum is
= |1-2| + |2-4| + |4-8| + |8-1|
= 14
For the given array, we get the maximum value for
the sequence {1, 8, 2, 4}
= |1-8| + |8-2| + |2-4| + |4-1|
= 18
为了解决这个问题,我们必须贪心地思考如何才能最大化元素的差值,从而获得最大的和。仅当我们计算一些非常高的值和一些非常低的值(例如(最高-最小))之间的差时,这才有可能。这是我们必须用来解决这个问题的想法。让我们看一下上面的例子,因为序列{1,8,2,4}可能具有最大的差值,因为在这个序列中,我们将得到一些高差值((1-8 | = 7 | 8-2 | |)。 = 6 ..)。在这里,通过将8(最高元素)替换为1和2,我们得到了两个高差值。类似地,对于其他值,我们将在其他值之间放置下一个最高值,因为我们只剩下一个,即最后放置4个。
算法:为了获得最大和,我们应该有一个顺序,大小元素交替出现。这样做是为了获得最大的差异。
对于上述算法的实现->
1.我们将对数组进行排序。
2.通过从排序数组中选取一个最小元素和最大元素,并计算出该最终序列的一个矢量数组,来计算最终序列。
3.最后,计算数组元素之间的绝对差之和。
下面是上述想法的实现:
C++
// CPP implementation of
// above algorithm
#include
using namespace std;
int MaxSumDifference(int a[], int n)
{
// final sequence stored in the vector
vector finalSequence;
// sort the original array
// so that we can retrieve
// the large elements from
// the end of array elements
sort(a, a + n);
// In this loop first we will insert
// one smallest element not entered
// till that time in final sequence
// and then enter a highest element
// (not entered till that time) in
// final sequence so that we
// have large difference value. This
// process is repeated till all array
// has completely entered in sequence.
// Here, we have loop till n/2 because
// we are inserting two elements at a
// time in loop.
for (int i = 0; i < n / 2; ++i) {
finalSequence.push_back(a[i]);
finalSequence.push_back(a[n - i - 1]);
}
// If there are odd elements, push the
// middle element at the end.
if (n % 2 != 0)
finalSequence.push_back(a[n/2]);
// variable to store the
// maximum sum of absolute
// difference
int MaximumSum = 0;
// In this loop absolute difference
// of elements for the final sequence
// is calculated.
for (int i = 0; i < n - 1; ++i) {
MaximumSum = MaximumSum + abs(finalSequence[i] -
finalSequence[i + 1]);
}
// absolute difference of last element
// and 1st element
MaximumSum = MaximumSum + abs(finalSequence[n - 1] -
finalSequence[0]);
// return the value
return MaximumSum;
}
// Driver function
int main()
{
int a[] = { 1, 2, 4, 8 };
int n = sizeof(a) / sizeof(a[0]);
cout << MaxSumDifference(a, n) << endl;
}
Java
// Java implementation of
// above algorithm
import java.io.*;
import java.util.*;
public class GFG {
static int MaxSumDifference(Integer []a, int n)
{
// final sequence stored in the vector
List finalSequence =
new ArrayList();
// sort the original array
// so that we can retrieve
// the large elements from
// the end of array elements
Arrays.sort(a);
// In this loop first we will insert
// one smallest element not entered
// till that time in final sequence
// and then enter a highest element
// (not entered till that time) in
// final sequence so that we
// have large difference value. This
// process is repeated till all array
// has completely entered in sequence.
// Here, we have loop till n/2 because
// we are inserting two elements at a
// time in loop.
for (int i = 0; i < n / 2; ++i) {
finalSequence.add(a[i]);
finalSequence.add(a[n - i - 1]);
}
// If there are odd elements, push the
// middle element at the end.
if (n % 2 != 0)
finalSequence.add(a[n/2]);
// variable to store the
// maximum sum of absolute
// difference
int MaximumSum = 0;
// In this loop absolute difference
// of elements for the final sequence
// is calculated.
for (int i = 0; i < n - 1; ++i) {
MaximumSum = MaximumSum +
Math.abs(finalSequence.get(i)
- finalSequence.get(i + 1));
}
// absolute difference of last element
// and 1st element
MaximumSum = MaximumSum +
Math.abs(finalSequence.get(n - 1)
- finalSequence.get(0));
// return the value
return MaximumSum;
}
// Driver Code
public static void main(String args[])
{
Integer []a = { 1, 2, 4, 8 };
int n = a.length;
System.out.print(MaxSumDifference(a, n));
}
}
// This code is contributed by
// Manish Shaw (manishshaw1)
Python3
import numpy as np
class GFG:
def MaxSumDifference(a,n):
# sort the original array
# so that we can retrieve
# the large elements from
# the end of array elements
np.sort(a);
# In this loop first we will
# insert one smallest element
# not entered till that time
# in final sequence and then
# enter a highest element(not
# entered till that time) in
# final sequence so that we
# have large difference value.
# This process is repeated till
# all array has completely
# entered in sequence. Here,
# we have loop till n/2 because
# we are inserting two elements
# at a time in loop.
j = 0
finalSequence = [0 for x in range(n)]
for i in range(0, int(n / 2)):
finalSequence[j] = a[i]
finalSequence[j + 1] = a[n - i - 1]
j = j + 2
# If there are odd elements, push the
# middle element at the end.
if (n % 2 != 0):
finalSequence[n-1] = a[n/2]
# variable to store the
# maximum sum of absolute
# difference
MaximumSum = 0
# In this loop absolute
# difference of elements
# for the final sequence
# is calculated.
for i in range(0, n - 1):
MaximumSum = (MaximumSum +
abs(finalSequence[i] -
finalSequence[i + 1]))
# absolute difference of last
# element and 1st element
MaximumSum = (MaximumSum +
abs(finalSequence[n - 1] -
finalSequence[0]));
# return the value
print (MaximumSum)
# Driver Code
a = [ 1, 2, 4, 8 ]
n = len(a)
GFG.MaxSumDifference(a, n);
# This code is contributed
# by Prateek Bajaj
C#
// C# implementation of
// above algorithm
using System;
using System.Collections.Generic;
class GFG {
static int MaxSumDifference(int []a, int n)
{
// final sequence stored in the vector
List finalSequence = new List();
// sort the original array
// so that we can retrieve
// the large elements from
// the end of array elements
Array.Sort(a);
// In this loop first we will insert
// one smallest element not entered
// till that time in final sequence
// and then enter a highest element
// (not entered till that time) in
// final sequence so that we
// have large difference value. This
// process is repeated till all array
// has completely entered in sequence.
// Here, we have loop till n/2 because
// we are inserting two elements at a
// time in loop.
for (int i = 0; i < n / 2; ++i) {
finalSequence.Add(a[i]);
finalSequence.Add(a[n - i - 1]);
}
// If there are odd elements, push the
// middle element at the end.
if (n % 2 != 0)
finalSequence.Add(a[n/2]);
// variable to store the
// maximum sum of absolute
// difference
int MaximumSum = 0;
// In this loop absolute difference
// of elements for the final sequence
// is calculated.
for (int i = 0; i < n - 1; ++i) {
MaximumSum = MaximumSum + Math.Abs(finalSequence[i] -
finalSequence[i + 1]);
}
// absolute difference of last element
// and 1st element
MaximumSum = MaximumSum + Math.Abs(finalSequence[n - 1] -
finalSequence[0]);
// return the value
return MaximumSum;
}
// Driver Code
public static void Main()
{
int []a = { 1, 2, 4, 8 };
int n = a.Length;
Console.WriteLine(MaxSumDifference(a, n));
}
}
// This code is contributed by
// Manish Shaw (manishshaw1)
PHP
输出 :
18