#K12946. Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
Minimum Operations to Equalize Array Elements
You are given an array of integers. In one operation, you can increment or decrement any element by 1. The goal is to make all elements equal using the minimum number of operations. It can be proven that setting all the elements to the median minimizes the number of operations.
Input Format (stdin):
- The first line contains an integer T, the number of test cases.
- For each test case, the first line contains an integer n, the size of the array.
- The second line contains n space-separated integers, the elements of the array.
Output Format (stdout):
- For each test case, output a single integer representing the minimum number of operations required to make all elements equal, each on a new line.
Note: All formulas, where applicable, are displayed in LaTeX. For instance, the median is defined as \(\text{median} = arr[\lfloor n/2 \rfloor]\).
inputFormat
The input is given via standard input (stdin). The first line contains an integer \(T\) denoting the number of test cases. Each test case consists of two lines: the first line contains an integer \(n\) denoting the number of elements in the array, and the second line contains \(n\) space-separated integers.
outputFormat
For each test case, output the minimum number of operations required to make all the array elements equal. Each answer should be printed on a new line on standard output (stdout).
## sample1
5
3 7 1 9 4
12
</p>