We need to find the minimum number of operations to make all piles equal using two operations:
- Remove some coins from a pile and put them back into the box (i.e., decrease a pile's count).
- Take coins from the box and add to a pile (i.e., increase a pile's count).
We have infinite coins in the box? Actually Chef has a box full of infinite number of identical coins. So the assistant can take as many as needed from box or put back as many. So effectively, we can increase any pile arbitrarily, or decrease any pile arbitrarily, each counted as one operation? Wait: The operations:
- Pick some coins from any pile and put them back in Chef's coin box.
- Pick some coins from the Chef's coin box and put them on any one pile.
Each operation is: either remove some coins from a specific pile (any amount) or add some coins to a specific pile (any amount). They count each such pick as one operation, regardless of number of coins moved? According to problem, "minimum number of operations needed" suggests each act of picking coins (from a pile to box or from box to pile) is an operation. So if you want to adjust a pile to a target value X, you need either: if current > X, you remove excess coins (one operation? Or multiple? You could pick all excess coins at once? The operation says "Pick some coins from any pile and put them back". It does not specify you can pick only one coin at a time. It says "pick some coins". In typical problems, such operation allows moving any positive number of coins, and each such move is counted as one operation. So we can adjust each pile in at most one operation: if it needs to increase, one add operation; if it needs to decrease, one remove operation. Indeed, we can take all needed coins from box and add to pile in one go, or remove all excess coins in one go. So each pile can be fixed with at most 1 operation, depending on whether it already equals the target or not. However, note that if a pile is already equal to target, no operation needed. If it is not equal, we need exactly 1 operation (either add or remove) to set it to target (since we can do all needed adjustment in one operation). Thus, the number of operations required to make all piles equal to some value X is: the number of piles i such that A_i != X.
But wait: Is there any constraint that we cannot both add and remove on same pile? We only need one operation per pile to adjust to X. So min operations = N - (maximum frequency of any value among the A_i). Because we choose X to be an existing value that appears most often, then we need to change all other piles (one operation each). But is that always optimal? Could choosing a value not present in the array reduce the number of operations? For any X not equal to any A_i, then every pile requires one operation (since all piles != X), so operations = N. So that would be >= N - maxfreq. Since N - maxfreq <= N (and for any X not present, it's N). So indeed, the optimal is to choose X as the mode of the array (the most frequent value). So answer = N - max frequency.
But wait, check example: N=4, A = [1,2,3,4]. Frequencies: each appears once, maxfreq=1 => N - 1 = 3. Output 3 matches.
But is there any twist? Could there be a scenario where we can use a combination of operations to achieve equality with fewer than N - maxfreq operations? For instance, if we choose X that is not present, we need N ops, so not better. But what if we can do something like: by adjusting some piles, we might combine operations? Since each operation affects exactly one pile. So total number of operations is number of piles that need change. It seems straightforward.
But wait: Could we possibly adjust a pile in zero operations by using multiple operations on other piles? No, each pile must end with same number of coins. The only way a pile gets to the target is if we directly change it or leave it. We can't change a pile without an operation. So indeed each pile not already at target requires at least one operation. And we can achieve exactly one operation per such pile. So minimum operations = number of piles that are not equal to target. So minimize that by maximizing count of piles that already equal target. So choose target to be the value that appears most often.
But careful: is there any possibility to use fewer than N - maxfreq operations by choosing a target value that might allow some piles to be changed without an operation because they are already equal? That's exactly maxfreq. So answer is N - maxfreq.
Thus the problem is trivial? But why would they give constraints up to 10^5? That's just for reading and counting frequencies. Indeed it's a simple problem.
But wait, I'm double-checking: The two operations are: remove some coins from any pile and put them back in box; add some coins from box to any pile. Could we possibly change a pile by doing both add and remove? That would be two operations, which is worse. So not beneficial.
But what if we can take coins from one pile and put onto another pile indirectly via box? For example, we could remove coins from a pile and then later add them to another pile. That would be two operations (one removal, one addition) affecting two piles. Could that be better than changing each pile individually? Suppose we want to adjust two piles: one is above target, one below. Could we take excess from the above pile and give to the below pile, using only two operations? Actually, to move coins from pile A to pile B, we would need: remove coins from A (operation 1) -> goes to box; then add coins from box to B (operation 2). That's two operations, which is the same as separately decreasing A (1 op) and increasing B (1 op) = 2 operations. So no saving. Moreover, if we want to adjust multiple piles, each change requires at least one operation per pile that is modified (either increase or decrease). So the lower bound is number of piles that need to be changed. And we can achieve exactly that by performing one operation per changed pile. So indeed minimal = N - maxfreq.
But wait, is there any subtlety: If we choose target X that is not equal to any A_i, then all piles require change => N ops. So not optimal.
Hence answer is simply N - maximum frequency.
Now the problem statement: "minimum number of operations needed" - we derived that.
But is there any scenario where we could set target to some value, and by performing operations we might cause a pile to become equal without directly operating on it? No, because operations only affect the pile you choose; they don't affect others.
Thus solution: For each test case, read N, read array A, compute frequencies (e.g., using dictionary or array of size up to 100000). Then answer = N - max(count).
Edge cases: N=1 => maxfreq=1 => answer 0. Since piles already equal (only one pile, trivially equal). That's correct.
Constraints: Ai up to 1e5, N up to 1e5, T up to 10. So O(N) per test case fine.
But let's double-check sample: They gave output 3, matches.
Thus solution is trivial.
But wait, is there any twist regarding "Pick some coins from any pile and put them back in Chef's coin box." That operation reduces the pile. Could we possibly remove coins from a pile without counting as an operation? No.
Maybe there is nuance: The assistant can pick coins from a pile and put back into box, and also pick coins from box and put on any pile. But maybe he can pick coins from multiple piles in one operation? The description: "Pick some coins from any pile and put them back in Chef's coin box." This implies from a single pile. He picks some coins from "any pile" singular. So each operation is per pile. So yes.
Thus answer is N - max frequency.
But I recall a similar problem from CodeChef: "Minimum Operations" or "Piles", maybe the answer is N - maxfreq. Yes, there is a known problem.
However, I should check if there is any hidden constraint: Could the assistant use the box to "transfer" coins between piles using one operation? The wording says: "Pick some coins from any pile and put them back in Chef's coin box." That is removal. "Pick some coins from the Chef's coin box and put them on any one pile." That is addition. To transfer, you need two separate operations. So no.
Thus solution confirmed.
Now we need to implement solve() reading input, processing test cases.
Complexities: O(T * (N + max_A)) but we can just use a dictionary.
Potential pitfalls: large input, use fast I/O.
Implementation:
import sys
def main():
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
freq = {}
maxf = 0
for i in range(n):
a = int(next(it))
freq[a] = freq.get(a, 0) + 1
if freq[a] > maxf:
maxf = freq[a]
out.append(str(n - maxf))
sys.stdout.write("\n".join(out))
if name == "main":
main()
That should work.
But wait, is there any scenario where we might need to consider operations like "do nothing" for a pile that is already equal? That's fine.
Thus answer is as above.
But before finalizing, let's double-check reasoning with a couple more examples:
Example: N=5, A=[2,2,2,2,2] => maxfreq=5 => answer 0. Correct.
Example: N=5, A=[1,1,2,3,4] => maxfreq=2 => answer 3. Let's test: If target=1, we need to change piles 3,4,5 => 3 ops. If target=2, change piles 1,2,4,5 => 4 ops; target=3 => 4; target=4 =>4; target=anything else =>5. So min=3. Good.
Example: N=6, A=[1,1,1,2,2,2] => maxfreq=3 => answer 3. If target=1, change three 2's -> 3 ops; target=2, change three 1's -> 3 ops; both equal. So answer 3. Could we do better by making target=1.5? Not possible because coins are integer counts? Actually coins are integer, so number of coins must be integer. So target must be integer. So yes.
Thus answer is correct.
Thus the problem reduces to finding the mode frequency.
I'll produce final answer accordingly. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
freq = {}
max_freq = 0
for _ in range(n):
x = int(next(it))
cnt = freq.get(x, 0) + 1
freq[x] = cnt
if cnt > max_freq:
max_freq = cnt
out_lines.append(str(n - max_freq))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()