-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaximum_sum_of_absolute_difference_of_any_permutation.cpp
66 lines (56 loc) · 1.51 KB
/
Maximum_sum_of_absolute_difference_of_any_permutation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Problem Statement:
------------------
Given an array, we need to find the maximum sum of absolute difference of any permutation of the given array.
Examples:
---------
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
*/
// Link --> https://www.geeksforgeeks.org/maximum-sum-absolute-difference-array/
// Code:
#include <bits/stdc++.h>
using namespace std;
int MaxSumDifference(int a[], int n)
{
vector<int> v;
sort(a, a + n);
int sum = 0;
// Alternatively inserting smallest and largest element
// to vector to get a zig-zag kind of array.
for (int i = 0; i < n / 2; i++)
{
v.push_back(a[i]);
v.push_back(a[n - i - 1]);
}
// if number of elements are odd then inserting middle element.
if(n %2 != 0)
v.push_back(a[n/2]);
for(int i=0 ; i<n ; i++)
{
if(i == n-1)
sum += (abs(v[i] - v[0]));
else
sum += (abs(v[i] - v[i+1]));
}
return sum;
}
int main()
{
int a[] = {1, 2, 4, 8};
int n = sizeof(a) / sizeof(a[0]);
cout << "Maximum sum of absolute difference of any permutation is : " << MaxSumDifference(a, n);
}