Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
he chooses two elements of the array ai, aj (i ≠ j);
he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
Output
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
Examples
Input
2
2 1
Output
1
Input
3
1 4 1
Output
3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to find maximum number of equal array elements achievable via operations: choose i != j, increase ai by 1, decrease aj by 1. This operation preserves total sum of array because +1 and -1. Also it changes two elements, but they can be any. Essentially we can redistribute values arbitrarily? Not exactly because we can only move one unit from one element to another per operation, but we can repeat many times. So effectively we can transform the array into any other array of integers (since values are integers) with the same sum? But we need to consider constraints: we can only change two elements at a time, increment one and decrement another. This is like we can move units from one element to another, as long as we don't change total sum. Also each operation only transfers 1 unit. But if we can perform arbitrarily many, we can achieve any configuration of integers with the same sum, provided that the parity or something? Let's think: Given an array of integers, we can apply any integer vector of changes d (with sum 0) such that each d_i is integer and we can represent as combination of elementary moves of +1 on one, -1 on another. Since the set of such moves generate all integer vectors with sum zero, because the space of all integer combinations of (e_i - e_j) (for i != j) is exactly the set of integer vectors with sum 0. Indeed, any integer vector d with sum 0 can be expressed as combination of such basic moves, because the graph is complete, and we can move from one index to another. For example, we can take any positive component and move it to any negative component. So yes, the set of achievable configurations from a starting array a is exactly the set of integer vectors b such that sum(b) = sum(a). No further constraints: because we can always transfer from a to b by moving surplus units from those where b_i < a_i? Actually we need to consider that we can only decrease an element (by moving units away) and increase others. But we cannot make an element negative? There is no lower bound? The operation is aj - 1; but ai, aj are integers, they could become negative? The problem statement does not restrict the values to be non-negative; it says |ai| ≤ 10^4, but after operations they could become any integer? Usually there is no lower bound, but we can apply operation an infinite number of times, and we can get arbitrarily negative values? However, we want to maximize the number of equal elements. Possibly it's beneficial to have some elements very negative or positive? But the operation can be done arbitrarily; there is no constraint that values remain within some bound. So indeed we can adjust arbitrarily.
Thus the problem reduces to: Given an array a of length n, we can transform it into any integer array b with the same total sum S. Among all such arrays b, what is the maximum possible number of indices i such that b_i = X for some integer X? In other words, we want the maximum possible multiplicity of a value achievable by redistributing the total sum among n elements (with integer values). Since we can set b_i to any integer as long as sum is S, the question is: For a given target value X, can we assign k elements equal to X, and the remaining n-k elements arbitrary integers (can be anything) such that sum of all = S? That is: kX + sum_rest = S. Since the rest can be any integers, we can always adjust them to make sum_rest = S - kX, provided that there exists an integer assignment for the remaining n-k elements that sum to that value. There is no other constraint; they can be arbitrarily large positive or negative. So for any integer X and any k from 0 to n, we can realize such configuration as long as we can set the remaining n-k numbers to sum to S - kX, which is always possible if we can assign any integer values to them. Since we have no bounds, we can always do that: e.g., set one of them to be (S - kX) and all others 0, or distribute arbitrarily. But note: They must be integers, but that's fine because S - k*X is integer. So for any X and k, we can find an integer vector for the remaining n-k elements that sum to the required total. However, is there any hidden constraint? Possibly that the numbers must be integers (they are). So indeed, for any integer X, we can make any number of elements equal to X, as long as we can adjust the rest to match sum. But there might be an additional subtlety: The operations we perform must be finite, but since we can do infinite operations, we can achieve any integer configuration with the same sum. No constraints like non-negativity.
Thus it seems we can make all n elements equal if and only if we can set all to the same integer X such that n*X = S. This requires that S is divisible by n? Actually X = S/n must be integer. But if S is not divisible by n, can we make all n equal? Let's see: If we want all elements equal to X, then sum = nX must equal S. Since X must be integer (values are integers), we require S divisible by n. If S is not divisible by n, then it's impossible to have all n equal integers. But maybe we can make all equal to a rational? No, values must be integers. So maximum number could be n if n divides S; otherwise maybe n-1? Let's test: Example 1: n=2, a=[2,1], sum=3. n=2, sum=3, 3/2 not integer. So all 2 equal would require both equal to 1.5? Not integer. Could we have both equal to 1? Sum would be 2, not 3. Both equal to 2 sum=4. So can't have both equal. So max equal count is 1. That matches output 1.
Example 2: n=3, a=[1,4,1], sum=6. n=3, 6/3=2 integer. So we can set all three to 2. Is it achievable? Yes: start from [1,4,1], we can move 2 from the 4 to the 1's: e.g., first operation: increase first 1 to 2, decrease 4 to 3; second: increase third 1 to 2, decrease 3 to 2. So all become 2. Output 3, matches.
Thus conjecture: The maximum number of equal elements is n if total sum S is divisible by n; otherwise it's n-1? Let's test some edge cases: n=1. Then we have just one element. What's max equal elements? That's 1, obviously. Sum S = a1. n=1, 1 divides any integer, so condition holds: n divides S? Actually S/n = a1 integer, so yes. So max = 1. That seems consistent.
But is it always possible to get n-1 equal elements when S is not divisible by n? Let's test: n=2, sum=3. We could set one element to something and the other equal? Want both equal? That fails. But we can have one element equal to some X, and the other not equal. The maximum number of equal elements would be 1. That's n-1=1, yes. n=3, suppose sum=5. 5/3 not integer. Can we get 2 equal elements? For example, can we make two elements equal to some integer X, and the third something else? The sum must be 5. So we need two X + y = 5 => y = 5 - 2X. For any integer X we can choose y accordingly. So yes, we can set two elements to X and third to 5-2X. As long as we can achieve with operations? Since we can arbitrarily assign any integers with same sum, we can. So 2 equal elements is possible. Is it possible to have 3 equal? No. So maximum = 2 = n-1. n=4, sum=1. Can we have 3 equal? Let's try: need three X + y = 1 => y = 1-3X. For any integer X, we can choose y. So yes, we can set three equal to X and one equal to 1-3X. So 3 equal possible. Could we have all 4 equal? Need 4X=1 -> X=0.25 not integer, impossible. So max = 3 = n-1.
But wait, is there any constraint that the numbers must be integers? Yes. So for n-1 equal elements, we can always choose some X integer such that the remaining one is integer. Since y = S - (n-1)X. For any integer X, y is integer. So always possible. So indeed, we can always achieve at least n-1 equal elements. However, is there any scenario where we can achieve n equal only if S%n==0? That seems to be the only condition.
But consider n=0? Not relevant.
Thus answer seems to be: if sum % n == 0 then n else n-1. But let's test with some other examples: Could there be a scenario where we can't even achieve n-1? Suppose n=2, sum odd, we get 1 equal, which is n-1. That's fine. For n=1, sum%1==0, answer n=1. That's consistent.
But wait, the problem allows negative numbers as well. Does that change anything? The sum S could be negative, but divisibility condition still holds: if S % n == 0? In many programming languages, modulo with negative numbers may be defined differently. But we need integer equality: Is there an integer X such that nX = S? That's exactly S divisible by n in the integer sense, i.e., S mod n = 0 (taking remainder in the mathematical sense: S = nq for some integer q). So condition: S is a multiple of n. That holds if S % n == 0 in C++ with negative numbers? For negative S, the remainder may be negative or positive depending on language. But we can just compute S mod n carefully: we need to check if S is divisible by n: i.e., S / n integer (exact division). In integer arithmetic, we can check if S % n == 0 (provided we use a language where % yields remainder with sign of dividend? In many languages, negative modulo yields negative remainder, but condition (S % n == 0) still works because if S is multiple of n, remainder is 0. For example, S=-6, n=3 => -6%3=0. So it's okay.
But is it always possible to get n-1 equal elements even if n>1? Let's think deeper: We need to transform original array into target array b. Since we can achieve any integer configuration with same sum, it's sufficient to show that there exists a target with n-1 equal elements and arbitrary sum. As argued, we can pick any integer X, and set b_1 = b_2 = ... = b_{n-1} = X, and b_n = S - (n-1)X. That's a valid integer array. So we can always achieve it.
Thus answer is trivial: if total sum divisible by n then n else n-1.
But wait, is there any limitation that we must apply the operation simultaneously on two distinct indices? That's the only operation. However, we assumed we can achieve any integer vector with sum zero. But is that indeed always achievable? Let's think: Starting from a, we want to reach b. The difference d_i = b_i - a_i has sum zero. The question: can we decompose the vector d into elementary moves of (+1 on i, -1 on j)? That is equivalent to whether we can transform via a sequence of such moves. This is classic: as long as we have at least two indices, we can always achieve any integer vector with sum zero. Because we can treat it as moving "units" from positions with surplus to deficits. For example, while there exists i with d_i > 0 and j with d_j < 0, we can apply operation: increase a_i by 1 (i.e., move one unit from surplus to deficit? Actually we want to increase b_i? Wait: operation: increase ai by 1, decrease aj by 1. So if we have surplus (d_i >0) we need to increase a_i? Actually d_i = b_i - a_i. If b_i > a_i, we need to increase a_i by b_i - a_i. So we need to apply (increase ai) operations to those indices. Conversely, if b_i < a_i, we need to decrease a_i. So we can pair a surplus index (needs to increase) with a deficit index (needs to decrease). Each operation increases one and decreases another. So as long as we have at least one surplus and one deficit, we can transfer one unit. After finite steps we can achieve the target. There's no limitation on how many times we increase/decrease the same index. So it's always possible.
But what if all d_i are zero? Already done. So it's always possible. So indeed any b with same sum is reachable.
Thus answer seems to be n if S divisible by n else n-1. However, we must verify with more complex cases: n=3, sum=7. Then answer n-1=2. Can we always get 2 equal? For example, can we get two elements equal to 3 and third equal to 1? Sum=3+3+1=7. Yes. Could we get two equal to 0 and third 7? Yes. So any X works.
But is it possible that we can get n equal even if sum not divisible by n? Suppose n=3, sum=4. Could we have all three equal to 1? That's sum 3, not 4. 2? sum 6. So no integer. So no.
Thus answer appears trivial.
But wait, why would the problem be given as Codeforces problem? Possibly there is a twist: The operation simultaneously increases one and decreases another, meaning total sum remains constant. But also maybe the operation must be performed on two distinct elements, and we cannot change more than two at once. That's fine. So the answer is as described.
But maybe there is a nuance: The array size can be up to 10^5, values up to |10^4|, but that doesn't matter.
I recall a Codeforces problem: "Polycarpus and Array" maybe? Let's search memory: There is a problem "258A - Little Elephant and Bits"? Not that. Actually I recall problem "Polycarpus and Dice" or something. But I'm not sure.
Let's double-check the statement: "Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times: he chooses two elements of the array ai, aj (i ≠ j); he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1. The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times. Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus." This is reminiscent of Codeforces Round #... I think it's "276B" or something? Let's recall: There is a problem "Polycarpus and Coins"? Not sure.
Anyway, we have a simple answer: if total sum % n == 0 then n else n-1.
But before finalizing, let's consider if there is any scenario where we can achieve n equal even if sum not divisible by n if we allow the equal value to be non-integer? No, because array elements must be integers. So no.
But is there any scenario where we can't even achieve n-1? For n=2, sum odd: we can have one equal? But what does "number of equal array elements" mean? They ask maximum number of equal array elements. For instance, in an array of size 2, if we have two equal elements, that's 2 equal. If we have one equal? Actually "number of equal array elements" means the count of elements that are all equal to each other. For an array with [1,2], we have no pair of equal elements? But the phrase "maximum number of equal array elements" likely means the size of the largest subset of indices that all have the same value. So in [1,2], we can have at most 1 (since each element alone can be considered a set of equal elements? Usually they mean the multiplicity of the most frequent value. So yes, maximum count of identical numbers. So if we can make two elements equal, answer 2; if not, maybe 1. So our interpretation stands.
Now, can we always achieve at least n-1? Suppose n=2, we can make both equal only if sum even. If sum odd, we can't make both equal. Can we make one element equal? Actually the array after operations will have two numbers; we want the maximum frequency. If they are not equal, the frequency is 1. So maximum possible is 1, which is n-1=1. So yes.
For n=3, if sum not multiple of 3, we can make two equal. Could there be a scenario where we cannot make two equal? Let's test: we need to find integers x,y such that after operations, two elements are equal to some value t, and third is something else. Since we can set any configuration with same sum, we can. So always possible.
But wait, there might be a hidden constraint: The operation changes exactly two distinct elements each time. It does not allow us to change a single element by itself. But we can achieve any configuration as argued.
Thus answer is straightforward.
But why would the problem be given if it's so trivial? Maybe I'm missing something. Let's re-read the problem statement carefully: "he chooses two elements of the array ai, aj (i ≠ j); he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1." This operation increments one and decrements another. So it's like moving one unit from j to i. So total sum constant. Yes.
Now, think about if there are any additional restrictions like we cannot make an element negative? Not stated. Could it be that values are integers, but maybe they cannot become negative because they are numbers? Usually there is no such restriction. The problem says "array of integers", and absolute values up to 10^4 initially, but after operations, they could be any integer. So no bound.
Maybe the twist is that the operation must be performed on two distinct elements. But that's fine.
Thus answer is trivial.
But I recall a Codeforces problem "B. Polycarp and Letters" etc. Not this.
Let's search my memory: There is Codeforces Round 86 (Div 2) problem "A. Road To Zero"? No.
Alternatively, maybe the problem originally had a twist: You cannot change the parity or something? But not mentioned.
Let's test with random cases. n=4, array: [1,2,3,4], sum=10. 10%4=2, so not divisible. According to our formula, maximum equal elements = n-1 = 3. Can we make three equal? Suppose we want three elements = t, and fourth = 10-3t. Need t integer. For t=2, fourth=10-6=4, so array [2,2,2,4] has three 2's. Achievable? Starting from [1,2,3,4], we can move units to make three 2's? Let's try: we need to increase 1 to 2 (+1), increase 3 to 2? Actually we need to make 3 to 2 (-1), and 4 to 4? Actually we want final [2,2,2,4]. Let's see differences: a1: +1, a2: 0, a3: -1, a4: 0. So we need to move one unit from a3 to a1. That's possible: do operation on a1 (increase) and a3 (decrease). So yes.
What about t=3? Then fourth = 10-9=1, array [3,3,3,1] three 3's. Differences: from [1,2,3,4] to [3,3,3,1] would require a1:+2, a2:+1, a3:0, a4:-3. We can move units: e.g., move 2 from a4 to a1, and 1 from a4 to a2. So possible.
Thus works.
So answer seems correct.
But wait, there might be a nuance: The maximum number of equal elements might be n if we can make all equal, but maybe also n if sum divisible by n. However, could there be cases where we can make all equal even if sum not divisible by n by using fractions? No.
Double-check with n=0? No.
Thus solution is simple: compute sum, if sum % n == 0, print n, else print n-1.
But is there any scenario where n-1 is not achievable because the remaining single element must also be integer? Yes, it's integer. So okay.
But what if n=1? Then n-1 =0, but answer should be 1. Our condition: if sum%1==0 always true, so we print n=1. So it's fine.
Edge: n=0 not allowed.
Thus answer is straightforward.
Now, the problem might be from Codeforces Round #... Usually such trivial solution would be for A problem. But I recall a problem "A. Little Elephant and Array" etc. Actually I think it's Codeforces 382A? No.
Anyway, let's produce final answer.
But hold on: There might be a catch: The operation is simultaneously increase one and decrease another. It changes two elements. Could there be any parity or other invariant besides sum? For instance, sum of squares? But that is not invariant; it changes by 2*(ai - aj) + 2? Actually (ai+1)^2 + (aj-1)^2 = ai^2+2ai+1 + aj^2-2aj+1 = ai^2+aj^2+2(ai-aj)+2. So sum of squares changes. No invariant.
What about sum modulo something? Sum is invariant, but perhaps also parity of sum? No, sum is invariant integer.
Thus only sum matters.
Maybe the problem originally had constraints that operations can't make elements negative? Let's check if that changes anything. Usually problems about redistributing values often have a constraint that values must remain non-negative (like balancing). But here it says "array consisting of n integers" with absolute value ≤ 10^4, but doesn't restrict after. If there was a non-negativity constraint, then the problem becomes more interesting: maximum number of equal elements if you can only move from positive to negative? But not stated.
I will read the statement again: "Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times: he chooses two elements of the array ai, aj (i ≠ j); he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1. The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times." There's no mention of bounds after operations. So it's safe.
Thus answer is as described.
But let's see example: Input:
2
2 1
Output: 1
Our formula: sum=3, n=2, 3%2 !=0 so n-1=1, correct.
Second: 3
1 4 1
sum=6, 6%3=0 => 3, correct.
Now, test with n=3, a = [0,0,1], sum=1, not divisible => n-1=2. Can we get two equal? Try to get two zeros? Already we have two zeros. So answer 2. Is it possible to get two ones? Let's see: we need two ones and one -1? Actually sum=1, to have two ones, we need third = -1. So we can get [1,1,-1] from [0,0,1] by operations: increase first 0 to 1 (so need +1), decrease third from 1 to -1 (need -2). Could do: first operation: increase first, decrease third => [1,0,0]; second: increase second (to 1) decrease third (from 0 to -1) => [1,1,-1]. So yes two ones. So max is 2.
What about [1,2,3] sum=6 divisible by 3 => answer 3, we can get all 2.
Thus seems consistent.
Now, maybe there is a more subtle condition: The operation is "increase ai by 1 and decrease aj by 1". That is like moving 1 from j to i. This preserves the sum modulo something? Actually, it preserves sum exactly.
Therefore answer is simply based on divisibility.
But why would they ask for "maximum number of equal array elements" if the answer is either n or n-1? It's almost trivial. Possibly there is a twist: The answer could be n if the sum is divisible by n, else maybe n - (sum mod n) ??? No, because we can always get n-1. But maybe there is a case where we can get n but not n-1? That's impossible because if we can get n, that's higher. So max is at least n-1.
But could it be that sometimes we cannot get n-1 because the sum S is such that for any X integer, the remaining element y = S - (n-1)X must be integer, which is always true. So it's always possible. So minimum guarantee is n-1.
Thus answer is n if S mod n == 0 else n-1.
Now, we should think about if there is any scenario where we could get more than n? No.
Thus solution is O(n) time, compute sum.
But wait, is there any constraint that we must use exactly the operation, and we cannot change the array in a way that is not reachable due to parity? Let's check parity: Since each operation adds 1 to one and subtracts 1 from another, the sum of all elements mod something might be invariant? Only sum is invariant. Parity of sum? Sum mod 2 is invariant because we add 1 and subtract 1, net change 0, so parity of sum is invariant. However, that's already captured by sum being constant. But that doesn't restrict anything else.
Maybe there is an invariant like sum of elements modulo something else? No.
What about the sum of elements taken modulo 2? Already covered.
Thus answer stands.
Nevertheless, I recall a problem "Polycarpus and Coins" where you have to make all numbers equal but you can only increment and decrement two elements, and the answer is something like: if sum mod n == 0 then n else n - (sum mod n)??? Not that.
Let's search memory: There's a Codeforces problem "A. Little Pony and Sort by Shift" etc.
Maybe this problem is from Codeforces Round #... Actually, I recall a problem named "Equalize" (maybe 103B?). Not.
I'll check quickly with my memory: There is a problem "B. Polycarpus and Coins" where you need to pay with coins and minimize... no.
Another thought: Could there be a scenario where we cannot achieve n-1 equal because we need to keep all numbers integers? That's fine. But maybe there is a restriction that the operations can be performed only a finite number of times but infinite allowed, so that's fine.
Wait, the problem might be from a contest where the solution is indeed simple, maybe for Div2 A.
Thus I'll go with that.
But before finalizing, let's test with a tricky case: n=3, a=[-1,0,1], sum=0. 0%3=0, so answer =3. Can we make all equal? Need all equal to 0. Starting from [-1,0,1], we can increase -1 to 0 (by +1) and decrease 1 to 0 (by -1) in one operation: choose i=1 (increase), j=3 (decrease). Then array becomes [0,0,0]. So yes.
Another: n=3, a=[-1, -1, 2], sum=0, divisible -> 3. Can we make all 0? Need to increase -1's to 0 (+1 each), decrease 2 to 0 (-2). We can do two operations: increase first -1 to 0 by taking from 2 (2->1), increase second -1 to 0 by taking from 1 (1->0). So final [0,0,0]. Works.
Now, what about n=4, a=[10000, -10000, 0, 0]? sum=0 divisible by 4 -> answer 4. We need all 0. Can we? Yes: move from 10000 to -10000: decrease 10000, increase -10000, but we want to bring all to 0. We can do operations: For instance, increase -10000 to 0 by taking 10000 from the 10000? Actually we need to add 10000 to -10000 to make it 0, so we need to decrease 10000 by 10000. That's one operation? No, operation increments one and decrements another by 1 each time. So to move 10000 units, we need 10000 operations. That's allowed (infinite). So possible. So all good.
Thus answer stands.
Now, I should consider if there is any scenario where the maximum number of equal elements is less than n-1. For instance, could it be that n-1 equal is impossible because the remaining one element must be equal to the others to have sum? But we can have n-1 equal to some X, and the last is Y = S - (n-1)X. There is no requirement that Y be different from X; if Y equals X then we have n equal, which is the divisible case. So if not divisible, Y will be different from X for all integer X? Let's check: If we set X such that Y = X, then S - (n-1)X = X => S = nX, which is divisible case. So in non-divisible case, for any X, Y != X, so indeed we have exactly n-1 equal (or maybe if we choose X such that some other coincidence? But still, the set of n-1 equal are the ones we set, and the last is different, so the max frequency is n-1. However, could it be that we could achieve a configuration where the frequency is n, i.e., all equal, even if sum not divisible? That would require all values equal to some integer X, but then sum = nX, which contradicts sum not divisible. So impossible.
Thus answer is indeed either n or n-1.
Now, is there any possibility to have more than n equal? No.
Thus final answer: if sum % n == 0, print n; else print n-1.
But wait, maybe there is a nuance: The operation is simultaneous increase and decrease, and it's possible that the array after many operations may have multiple equal values, but we are asked the maximum number of equal elements achievable. Could there be a scenario where we can have a frequency higher than n-1 but less than n? No, frequency is integer. So only n or n-1.
Thus solution seems trivial.
But perhaps there is a twist: The operation must be applied to two distinct elements, and we cannot change more than two at once. However, we can also apply operation to same pair multiple times. So fine.
Thus the solution is straightforward.
Now, I'll implement: read n, read array, compute sum (use 64-bit integer because n up to 1e5, |ai| up to 1e4, so sum up to 1e9, fits in 32-bit? Actually 1e5 * 1e4 = 1e9, which fits in 32-bit signed? 2^31-1 = 2.147e9, so fits. But safe to use long long.
If sum % n == 0: print n; else print n-1.
But wait, is there any case where n-1 equals 0? For n=1, n-1=0, but we would go to else? But for n=1, sum%1==0 always, so we go to if branch and print 1. So fine.
Thus answer.
But I recall that in some Codeforces problem, the answer was n if all numbers already equal? But no.
Now, let's double-check if there is any additional invariant such as parity of count of odd numbers? Because the operation changes parity of both selected elements: increasing by 1 flips parity, decreasing by 1 flips parity. So parity of both change. So the total number of odd numbers can change parity? Let's see: if both ai and aj are even, after operation: ai+1 odd, aj-1 odd => number of odd numbers increases by 2. If both odd: ai+1 even, aj-1 even => number of odd decreases by 2. If one even, one odd: say ai even, aj odd: ai+1 odd, aj-1 even => number of odd remains same (even->odd, odd->even). So the parity (mod 2) of count of odd numbers can change? Actually the parity of the count of odd numbers can change? Let's compute: initially count O. After operation: cases:
both even: O -> O+2, parity unchanged (since +2 even).
both odd: O -> O-2, parity unchanged.
one even, one odd: O -> O (since even becomes odd, odd becomes even). So parity unchanged. So parity of O is invariant! Actually O mod 2 remains same because changes are multiples of 2 or 0. So O mod 2 is invariant. Similarly, sum mod 2? Sum is constant, so its parity is invariant. But sum parity is S mod 2, which is not necessarily same as O mod 2? Might be related: sum mod 2 equals number of odd elements mod 2 because each odd contributes 1 mod 2, even contributes 0. So S mod 2 ≡ O mod 2 (since sum mod 2 = (sum of (ai mod 2)) mod 2 = O mod 2). So indeed both invariants are consistent.
Thus there is no extra constraint beyond sum.
Now, could there be an invariant mod something else? For example, sum mod something? Only sum is constant; we can change values arbitrarily as long as sum stays same.
Thus fine.
Now, finalize.
But before finalizing, let's search memory: I think there is a Codeforces problem named "Polycarp and Sums of Subsequences" not.
Maybe it's problem "Equalize" from Codeforces Round #... but that one has different operation (swap). No.
Anyway, the solution is as derived.
But maybe the problem statement is simplified and originally had constraints like you can only apply operation if both numbers are positive? Not here.
Thus I'll answer with this.
But wait, what about n=0? Not possible.
Thus final.
But we should also consider the possibility that maximum number of equal elements might be n if all elements already equal? No, that's covered.
Ok.
Now, let's produce the final output.
But I'm a bit uneasy: The problem is too trivial for a Codeforces problem? Perhaps it's from a very early round or a Div2 A, which are often trivial. For example, Codeforces Round #... Actually I recall problem "A. Vasya and Socks" etc. Not.
Let's check: The input constraints n up to 1e5, values |ai| <= 1e4. That's typical for simple solution.
Thus I think it's correct.
Double-check with more random cases: n=5, sum=23. 23%5=3, so answer =4. Can we get 4 equal? For some X, we need Y = 23 - 4X. Choose X=5 => Y=3 => [5,5,5,5,3] frequency 4. Achievable? Yes.
What about X=6 => Y=-1 => [6,6,6,6,-1] also 4. So always.
What about X=0 => Y=23 => [0,0,0,0,23] frequency 4.
Thus fine.
Now, maybe there is a scenario where we could get n equal even if sum not divisible, by making them equal to a non-integer? No.
So answer is correct.
Thus implementation: read n, read array, compute sum, if sum % n == 0 print n else print n-1.
But careful: In languages where % may return negative remainder, but we want to check if divisible, i.e., remainder 0. That works even if sum negative: e.g., sum = -6, n=3, -6%3 = 0 in most languages (C++: -6%3 = 0). Python: -6%3 = 0. So fine.
Now, what about n=0? Not.
Thus final.
But let's also consider if n=2, sum=0, divisible, answer 2. Example: [1,-1] can become [0,0]? Starting from [1,-1], we can increase -1 by 1 (to 0) and decrease 1 by 1 (to 0) in one operation: increase ai (say -1) and decrease aj (1) => [-1+1=0, 1-1=0] => [0,0]. So yes.
Now, what about n=2, sum=1, answer 1. Could we get array [0,1]? That's original maybe [2,-1] sum=1, we want two equal? Not possible. So max 1.
Thus consistent.
Thus I'm confident.
Now, produce final answer in required format.
But wait, the problem might require to print the maximum number of equal elements after performing operations. Could there be a scenario where we can get more than n-1 but not n? No.
Thus final solution.
I'll now craft the final response: explanation and code.
But the user asked: "Polycarpus has an array, ... Help Polycarpus." They expect an answer, presumably the solution explanation and code. The conversation: They posted the problem statement and said "Help Polycarpus." So likely they want solution.
Thus I will provide reasoning and then code in C++ (since it's Codeforces style). But also note that the user might want any language? Typically Codeforces uses C++/Java/Python. I'll provide a C++ solution.
But also, as a reasoning: sum remains constant; we can achieve any distribution with same sum; so if total sum divisible by n, we can make all n equal; else we can make n-1 equal.
I'll explain that we can always achieve at least n-1 equal by setting n-1 numbers to any integer X and the last to S - (n-1)X. And because we can't get all n equal if S not divisible by n, the maximum is n-1.
Thus final answer.
But is it always true that we can achieve n-1? What if n=2? That gives 1, fine. n=3 gives 2. For n=1, we can't have n-1=0 equal because we need at least one element? But we treat separately.
So answer.
Now, code:
#include
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
}
if (sum % n == 0) {
cout << n << endl;
} else {
cout << n-1 << endl;
}
return 0;
}
But careful: n is up to 1e5, sum might be large: up to 1e9, fits in 64-bit.
Now, final.
But I'm thinking: Could there be a scenario where n-1 is not achievable because we need to keep the array values as integers? Already done. But what if n is 0? No.
Thus final.
I'll write the answer. The answer depends solely on the total sum of the array. Because each operation adds 1 to one element and subtracts 1 from another, the total sum remains constant. Moreover, any integer array with the same sum can be reached by a sequence of such operations (by moving units from elements that are too high to those that are too low).
Therefore, if the total sum (S) is divisible by (n), we can make all (n) elements equal to (S/n). If not, we cannot make all (n) equal (they would have to be non‑integer). However, we can always make (n-1) elements equal to some integer (X) and set the remaining element to (S-(n-1)X), which is always an integer. Hence the maximum number of equal elements is (n) when (S) is a multiple of (n), and (n-1) otherwise.
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
sum += x;
}
if (sum % n == 0)
cout << n << endl;
else
cout << n - 1 << endl;
return 0;
}