maximum-difference-5

Given N space separated integers. Your task is to arrange them such that the summation M of the absolute differences between every two adjacent numbers is maximum.  

Input:

First line of the input contains an integer N.      
Second line contains N space separated integers A_i.   

Output:

Print the above described M. 

Constraints:

1 ≤ N ≤ 1000.
1 ≤ A_i  ≤ 10^{5}.

SAMPLE INPUT
3
1 2 7

SAMPLE OUTPUT
11

Explanation

One of the best arrangements is (1,7,2), M = |1 - 7| + |7 - 2| = 11

Difficulty: EASY
Tags: Sorting

Implementation
Skill types: Sorting

Starter code:

Solutions:
T=int(input())
arr=list(map(int,input().strip().split(" ")))
arr.sort()
i=0
j=len(arr)-1
sum1=0
count=0
while i<j:
	sum1+=arr[j]-arr[i]
	count+=1
	if count%2==1:
		i+=1
	else:
		j-=1
sum2=0
count=0
i,j=0,len(arr)-1
while i<j:
	sum2+=arr[j]-arr[i]
	count+=1

	if count%2==1:
		j-=1
	else:
		i+=1
if sum2<sum1:
	print(sum1)
else:
	print(sum2)



N = eval(input())
arr = input().split(' ')
ll = list()
ll2 = list()
i=0
for item in arr:
	ll.append(int(arr[i]))
	ll2.append(int(arr[i]))
	i = i + 1


count = 0
value = 0
total = 0
prev = min(ll)
while True:
	if count % 2 == 0:
		value = min(ll)
	else:
		value = max(ll)
		
	ll.remove(value)
	total = total + abs(value-prev)
	prev = value
	count = count + 1
	if count == i:
		break


count = 0
value = 0
total1 = 0
prev = max(ll2)
while True:
	if count % 2 == 0:
		value = max(ll2)
	else:
		value = min(ll2)
		
	ll2.remove(value)
	total1 = total1 + abs(value-prev)
	prev = value
	count = count + 1
	if count == i:
		break


if total >= total1:
	print(total)
else:
	print(total1)


n=int(input())
list_data=[]
data=input().split(' ')
for i in data:
	list_data.append(int(i))
list_data.sort(reverse=True)
if n%2==0:
	mid=n/2
else:
	mid=(n-1)/2
list1=(list_data[:mid])
list1.sort()
list2=list_data[mid:]
s=''
#print mid
#print list2
#print list1
for i in range(0,mid+1):
	try:
		s+=str(list2[i])+' '
	except:
		pass
	try:
		s+=str(list1[i])+' '
	except:
		pass

s=s.split(' ')[0:-1]
#print s
ans=0
l=len(s)
i=0
while i<=l-2:
#	print i
	ans+=abs(int(s[i])-int(s[i+1]))
	i+=1
print(ans)




n = int(input())
a = [int(i) for i in input().split()]

a.sort()

front = []
back = []

i = 0
j = n-1

small = []
big = []

while i < j :
	small.append(a[i])
	small.append(a[j])
	big.append(a[j])
	big.append(a[i])

	i+=1
	j-=1

	if i == j :
		small.append(a[i])
		big.append(a[i])
		break
	if i > j :
		break

x = 0
y = 0

for i in range(1,n) :
	x += abs(small[i] - small[i-1])
	y += abs(big[i] - big[i-1])

print(max(x,y))


n = int(input())
a = [int(i) for i in input().split()]

a.sort()

front = []
back = []

i = 0
j = n-1

small = []
big = []

while i < j :
	small.append(a[i])
	small.append(a[j])
	big.append(a[j])
	big.append(a[i])

	i+=1
	j-=1

	if i == j :
		small.append(a[i])
		big.append(a[i])
		break
	if i > j :
		break

x = 0
y = 0

for i in range(1,n) :
	x += abs(small[i] - small[i-1])
	y += abs(big[i] - big[i-1])

print(max(x,y))


def maxdif(N, A):
	A.sort()
	new1 = []
	new2 = []
	dif1 = []
	dif2 = []
	temp = None
	if N % 2:
		temp = A.pop(N / 2)
	while A:
		t1=A.pop(0)
		t2=A.pop(-1)
		new1.append(t1)
		new1.append(t2)
		new2.append(t2)
		new2.append(t1)
	if temp:
		new1.append(temp)
		new2.append(temp)
	for i in range(1, N):
		dif1.append(abs(new1[i] - new1[i - 1]))
		dif2.append(abs(new2[i] - new2[i - 1]))
	return max(sum(dif1),sum(dif2))


N = int(input())
A = list(map(int, input().split(' ')))

print(maxdif(N, A))



import math
n = int(input())

a = list(map(int,input().split()))

a = sorted(a)

b = []
c = []

for i in range(n/2):
	b.append(a[i])
	b.append(a[n-1-i])
	c.append(a[n-1-i])
	c.append(a[i])

if n%2 != 0 :
	b.append(a[n/2])
	c.append(a[n/2])

ans = 0
ans1 = 0
#print b,c
for i in range(n-1):
	ans += abs(b[i]-b[i+1])
	ans1 += abs(c[i]-c[i+1])

print(max(ans,ans1))















N = eval(input())
A = list(map(int, input().split()))
A1 = A[:]
A1.sort(reverse=True)
A.sort()
#print A, A1

final_list = []

i = j = 0
append_min = True
for I in range(N):
	if append_min:
		final_list.append(A[i])
		i+=1
		append_min = False
	else:
		final_list.append(A1[j])
		j+=1
		append_min = True
summ=0

for i in range(N-1):
	summ+=abs(final_list[i]-final_list[i+1])
#print summ

final_list1=[]
i = j = 0
append_max = True
for J in range(N):
	if append_max:
		final_list1.append(A1[i])
		i+=1
		append_max = False
	else:
		final_list1.append(A[j])
		j+=1
		append_max = True
summ1=0

for i in range(N-1):
	summ1+=abs(final_list1[i]-final_list1[i+1])
#print summ1

print(max(summ, summ1))