Fredo and Sums
Practice
3.3 (21 votes)
Algorithms
Arrays
Easy
Greedy algorithms
Merge sort
Sorting
Problem
66% Success 8483 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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\)

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

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
10 votes
Tags:
ApprovedEasyMathOpen
Points:30
16 votes
Tags:
AlgorithmsCounting and ArrangementsDivide-and-conquer algorithmMerge SortMerge sortSorting
Points:30
330 votes
Tags:
ApprovedEasyReadySorting