-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swap and Maximize.cpp
75 lines (56 loc) · 1.33 KB
/
Swap and Maximize.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
67
68
69
70
71
72
73
74
75
PROBLEM:
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|.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case contains the number of elements in the array a[] as n and next line
contains space separated n elements in the array a[].
Output:
Print an integer which denotes the maximized sum.
Constraints:
1<=T<=100
1<=n<=10000
1<=a[i]<=100000
Example:
Input:
2
4
4 2 1 8
3
10 12 15
Output:
18
10
SOLUTION:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,i,s=0;
cin>>n;
int a[n];
vector<int> v;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(i=0;i<n/2;i++)
{
v.push_back(a[i]);
v.push_back(a[n-i-1]);
}
if(n%2==1)
v.push_back(a[n/2]);
/*for(i=0;i<n;i++)
cout<<v[i]<<" ";
cout<<endl;*/
for(i=0;i<n-1;i++)
{
s+=abs(v[i+1]-v[i]);
}
s+=abs(v[n-1]-v[0]);
cout<<s<<endl;
}
return 0;
}