Editorial
Subtract from every value, so the initial values are . If , apply the reflection ; hence assume .
Let , , , , and . The final value must be the global average . After any number of operations, every reduced denominator is a power of two, so the task is impossible if is not a power of two.
Assume that is a power of two. Since , we have . Let . The minimum number of operations is .
Construction
If , then is odd. Make groups of size , each containing copies of , respectively. Split the remaining elements into groups of size .
Bound
Build a graph by joining two indices if they are ever used together, and let be its number of connected components. If a component has size , its final sum must be an integer, so is divisible by . Write the component sizes as ; then .
The total time complexity is .
Solution written by GPT5.6