Maximum Difference

3.8

541 votes
Implementation, Sorting, Approved, Easy
Problem

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 Ai.

Output:

Print the above described M.

Constraints:

  • 1N1000.
  • 1Ai105.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

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

Editor Image

?