#K78057. Sum of Absolute Differences
Sum of Absolute Differences
Sum of Absolute Differences
You are given T test cases. For each test case, you are provided with an integer N and a sequence of N integers. Your task is to compute the sum of the absolute differences between every pair of integers in each sequence.
More formally, for a given sequence \(a_1, a_2, \ldots, a_N\), compute:
[ S = \sum_{1 \leq i < j \leq N} |a_i - a_j| ]
For example, for the sequence [1, 2, 3], the answer is:
[ |1-2| + |1-3| + |2-3| = 1 + 2 + 1 = 4 ]
Please note that the input is provided via standard input and the output should be printed to standard output.
inputFormat
The first line of input contains an integer T, denoting the number of test cases. The description of the test cases follows.
For each test case:
- The first line contains an integer N, the number of integers in the sequence.
- The second line contains N space-separated integers.
Total input is read from stdin.
outputFormat
For each test case, output a single integer representing the sum of absolute differences between every pair of integers in the sequence. Each answer should be printed on a separate line to stdout.
## sample4
3
1 2 3
4
-1 -2 -3 -4
3
-5 0 5
3
100000 -100000 0
4
10
20
400000
</p>