#K68477. Equalize Array Elements in Minimum Moves

    ID: 32873 Type: Default 1000ms 256MiB

Equalize Array Elements in Minimum Moves

Equalize Array Elements in Minimum Moves

Given an array of integers, your task is to determine the minimum number of moves required to make all elements equal. In one move, you may increment or decrement any single element by 1. It turns out that the optimal strategy is to adjust the extreme values, and the minimum number of moves needed is simply the difference between the maximum and minimum elements of the array.

More formally, if \( m = \min_{1\le i \le n} a_i \) and \( M = \max_{1\le i \le n} a_i \), then the answer is \( M - m \). For an empty array, the output is defined to be 0.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains a single integer \( n \) representing the number of elements in the array.
  • If \( n \gt 0 \), the second line contains \( n \) space-separated integers which are the elements of the array.

outputFormat

Output a single integer to standard output (stdout) representing the minimum number of moves required to equalize the array elements.

## sample
3
3 3 3
0