forked from mathewsaju23/Hackoctober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max.py
67 lines (58 loc) · 1.5 KB
/
max.py
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
#check
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 + 1]
# 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
# bymathew