#K4206. Equalize the Circular Array
Equalize the Circular Array
Equalize the Circular Array
You are given a circular array of N integers. Your task is to determine the minimum number of operations required to make all the elements equal.
An operation is defined as transferring a unit from one element to its adjacent element (the array is considered circular, so the first and last elements are also adjacent). However, the challenge is structured in such a way that it is always optimal to balance the cumulative sum as you traverse the array.
Let \(T\) be the total sum of the array and \(M = \frac{T}{N}\) the target value for each element (it is guaranteed that \(T\) is divisible by \(N\)). As you iterate through the array, you maintain a running sum. If at any point the running sum differs from the expected sum \((k \times M)\) for some integer \(k\), an operation is needed. Your goal is to calculate the minimum number of such operations that result in every element being equal to \(M\).
Input format and output format details are provided below.
inputFormat
The input is read from standard input:
- The first line contains an integer \(N\) (the number of elements in the array).
- The second line contains \(N\) space-separated integers representing the elements of the array.
It is guaranteed that the total sum of the array is divisible by \(N\).
outputFormat
Output a single integer denoting the minimum number of operations required to make all elements equal.
## sample4
3 6 4 2
3
</p>