Fredo and Sums
Tag(s):

## Algorithms, Arrays, Easy-Medium, Greedy algorithm, Merge sort, Sorting

Problem
Editorial
Analytics

Fredo has an array A consisting of N elements. He wants to divide the array into $N/2$ pairs where each array element comes in exactly one pair. Say that pair i has elements $X_i$ and $Y_i$, he defines S as :

$S = \sum_{i=1}^{N/2} abs(X_i-Y_i)$

He asks you to find the minimum and maximum value of S.

Input Format:

First line consists of an integer T denoting the number of test cases.
Each test case:
First line consists of an integer N denoting the number of elements in the array.
Second line consists of N space separated integers denoting the array elements.

Output Format:
For each test case, print the minimum and maximum sum (space separated). Answer for each test case should come in a new line.

Input Constraints:

$1 \le T \le 10$
$1 \le N \le 10^5$
$-10^9 \le A[i] \le 10^9$
$N \ is \ even$

SAMPLE INPUT
1
4
10 20 -10 0

SAMPLE OUTPUT
20 40

Explanation

For minimum sum, we take pairs $(10,20)$ and $(-10,0)$.
For maximum sum, we take pairs $(20,0)$ and $(10,-10)$.

Time Limit: 1,0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

## CODE EDITOR

Initializing Code Editor...

## This Problem was Asked in Challenge Name

July Circuits '17

OTHER PROBLEMS OF THIS CHALLENGE
• Basic Programming > Implementation
• Algorithms > Searching
• Algorithms > Graphs
• Algorithms > Dynamic Programming
• Algorithms > Graphs
• Data Structures > Trees
Уведомления

?