Closed MuhammadTausif closed 4 hours ago
arr.sort()
l=[]
i=0
j=len(arr)-1
while(i<j):
l.append(arr[i])
l.append(arr[j])
i+=1
j-=1
if i==j:
l.append(arr[i])
s=0
for i in range(len(l)-1):
s+=abs(l[i]-l[i+1])
s+=abs(l[0]-l[-1])
return s
Swap and Maximize
Link
Difficulty: Easy
Given an array arr[ ] of positive elements. Consider the array as a circular array, meaning the element after the last element is the first element of the array. The task is to find the maximum sum of the absolute differences between consecutive elements with shuffling of array elements allowed i.e. shuffle the array elements and make [a1..an] such order that |a1 – a2| + |a2 – a3| + …… + |an-1 – an| + |an – a1| is maximized.
Examples:
Constraints: