WA in Dividing Array

ques: (it’s from our course dashboard)

You are given an array, A, of n elements. You have to remove exactly n/2 elements from array A, and add it to another array B(initially empty). After these operations, you can rearrange the elements of both the arrays in any order. We define, difference between these two arrays as :

∑ i = 1 N / 2 a b s ( A i − B i )
Find the maximum and minimum values of differences of these two arrays.

Input Format:
First Line contains number of test cases, T. For each test case, first line contains a single integer, n (n is even). Next line contains n integers, denoting the elements f array, A.

Constraints:
1<=T<=10 1<=n<=10^5 |Ai|<=10^9

Output Format
For each test case, print the minimum and maximum value of differences between two arrays.

Sample Input
1
4
12 -3 10 0
Sample Output
5 25

code: https://ide.codingblocks.com/s/58336

I think there is a minor mistake in the code but I can’t find it.
Logic : For max difference, always take max - min in every case
For min difference, always subtract min with next min and max with next max

typo

max with previous max

Hey Kunal, your approach is not correct, you can use greedy approach for this problem, then minimum and maximum sum expressions will be like this
45%20PM

well I am actually doing the same thing but with a bit more of code. Can u check which test case it gives WA.
For min : I am doing A1-A0 then An-1 - An-2 and do till n/2. I guess some corner cases are missing in my logic. Can u check that pls.

Check for this case
input:
1
10
746113975
-349360348
729136705
644545866
906164083
973748660
-216604078
-646300889
-377059304
-351182282

your code’s output:
2753324572 5940216190

but the expected output is:
1216775310 5940216190

Got it TYSM. My approach was a bit broken :blush: