#C2091. Minimum Moves to Equal Array Elements

    ID: 45369 Type: Default 1000ms 256MiB

Minimum Moves to Equal Array Elements

Minimum Moves to Equal Array Elements

Given an array of n integers, you are allowed to perform a move where you increment n-1 elements by 1. Equivalently, you can think of this operation as decrementing one element by 1. The task is to determine the minimum number of moves required to make all elements of the array equal.

Using a mathematical approach, if the array is \(a_1, a_2, \ldots, a_n\) and the minimum element is \(m = \min(a_1, a_2, \ldots, a_n)\), then the minimum number of moves needed is:

\[ \text{moves} = \sum_{i=1}^{n}(a_i - m)\ \]

Your goal is to compute this value for each given test case.

inputFormat

The first line contains a single integer T denoting the number of test cases.

Each test case consists of two lines:

  • The first line contains an integer n representing the number of elements in the array.
  • The second line contains n space-separated integers representing the elements of the array.

outputFormat

For each test case, output a single line containing the minimum number of moves required to equalize all array elements.

## sample
1
3
1 2 3
3

</p>