Maximum Difference
Practice
3.8 (541 votes)
Implementation
Sorting
Approved
Easy
Problem
73% Success 5501 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code
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 \le N \le 1000\).
- \(1 \le A_i \le 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\)
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Submissions
Please login to view your submissions
Similar Problems
Points:20
1 votes
Tags:
EasyNumber Theory
Points:20
207 votes
Tags:
ApprovedEasyMathOpenPrimality testPrime FactorizationReady
Points:50
11 votes
Tags:
Divide-and-conquer algorithmFast Fourier transformGenerating FunctionsMedium
Editorial
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor