You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
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 the length of the longest subsegment (contiguous subarray) that has the maximum possible arithmetic mean (average). The array size up to 1e5. The values up to 1e9.
We want the maximum average over all subarrays. Since we want the longest subsegment among those with that maximum average.
Important: The maximum average over all subarrays is simply the maximum element in the array? Not exactly. For non-negative numbers, the maximum average is indeed the maximum element because average of a subarray containing the maximum element alone is that element, and any other subarray might have lower average if it includes lower numbers. However, if there are negative numbers, the maximum average could be higher than max element? No, since the average of any subarray cannot exceed its maximum element, so the maximum average is at most the maximum element. Actually, consider subarray consisting only of the maximum element; its average equals the maximum element. So maximum average >= max element. And obviously cannot exceed max element because any subarray containing elements <= max element: average <= max element. So yes, the maximum average is exactly the maximum element. That holds for any array? Let's check: Suppose max element M. For any subarray, its sum is <= M * length, so average <= M. So maximum average = M. So trivial: the max average is the maximum value in the array. But wait: Are there cases where the maximum average is greater than the maximum element? No, impossible. So the problem reduces to: find the length of the longest contiguous subarray whose average equals the maximum element of the whole array. But careful: If all numbers are non-negative, yes. However, the problem statement says a_i >= 0 (0 ≤ a_i ≤ 10^9). So indeed all are non-negative. So the maximum average is max(a). Then the subsegments with average equal to the max element are exactly those subarrays where every element is equal to the max element? Not exactly: Suppose we have a subarray that contains the max element along with some other numbers that are less than max. Then average would be less than max because it's a weighted average. Unless the other numbers are also exactly max, the average will be less than max. So the only subarrays with average exactly equal to the maximum element are those consisting solely of the maximum element. Because if any element is less than max, the average becomes strictly less than max, given that all numbers are non-negative? Actually, consider subarray with mix: Suppose max=5, other numbers 4.9? But these are integers, so other numbers are <=4. So average would be less than 5. Indeed if there is any element less than M, the sum is at most M*(k-1)+(M-1) = M*k -1, average <= M - 1/k < M. So only subarrays composed entirely of M can have average exactly M. So the longest subsegment with maximum arithmetic mean is the longest run of consecutive occurrences of the maximum element in the array.
Thus answer is the length of the longest contiguous block of the maximum value.
But wait: The example: Input: 6 1 6 6 0. Maximum element is 6. Longest consecutive run of 6's? The array: indices:1:6,2:1,3:6,4:6,5:0 => runs: single 6 at index1 (length1), then at indices3-4 length2. So longest run is length2. Output2. Matches.
So solution is straightforward.
But is there any nuance? Suppose the array contains only zeros? Then max=0, any subarray average is 0. So then all subsegments have average 0, which is the maximum average. Among them, we need the longest subsegment. That would be the whole array, length n. But also the subsegment must have average exactly 0 (which is trivially true). However, if there are zeros and maybe positive numbers? Then max>0. So zeros only case: max=0. Then any subarray average is 0? Actually, if all numbers are 0, yes. But if there are zeros and also maybe positive numbers? Then max>0 so not relevant. So for all-zero array, answer is n. That fits the "longest run of max" which would be the entire array because max=0 and entire array is all zeros. So works.
But wait: Could there be a subarray that includes zeros along with the max element and still average = max? If max=0, then all numbers are 0. So yes. If max>0, zeros bring down average, so not possible. So our logic holds.
Thus answer: find maximum value M, then find the longest consecutive segment where a_i == M.
Complexity O(n).
But is there any trick when M = 0? Works.
Thus solution is trivial. But wait, the problem statement: "Find the subsegment ... with maximum arithmetic mean ... in floating-point numbers, i.e. without any rounding." So they want the average not rounded. That doesn't affect our reasoning.
But why would this problem be given? Possibly because some might think of using binary search on average and check with conversion to prefix sums, etc. But indeed with non-negative numbers it's trivial. However, if negative numbers were allowed, it would be more complicated. But constraints say a_i >=0. So it's simple.
Thus implement: read n, read list a. Compute max_val = max(a). Then iterate to find longest consecutive sequence of max_val.
Edge cases: n=1 => answer 1.
Ok.
But let me double-check: Is there any scenario where a subarray consisting of multiple copies of max and also some other numbers equal to max? Wait, other numbers less than max: If there are numbers that are also max? That's the same. The only way average equals max is if every element is max. Proof: Let subarray of length L with elements a_i, with max_val = M. For average to be M, we need sum = ML. Since each a_i <= M, sum <= ML with equality iff each a_i = M. So yes.
Thus solved.
Now code in Python.
I'll write:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
a = list(map(int, data[1:1+n]))
M = max(a)
max_len = 0
cur = 0
for x in a:
if x == M:
cur += 1
max_len = max(max_len, cur)
else:
cur = 0
print(max_len)
That's it.
But is there a nuance about "maximum arithmetic mean" being the maximum among all subsegments? That's what we used. However, if there are multiple subsegments with that same maximum average, we need the longest. In case all numbers equal, the longest is n. That's captured by run of max.
But wait: Could there be subsegments with average = M but not consisting entirely of M? Could there be a mix of numbers with average M if some numbers are greater than M? But M is the maximum overall, so no number greater than M. So no.
Thus final.
But sometimes problems like this have negative numbers, but here they don't.
I think it's correct.
I'll provide explanation: the maximum average is max element; longest subsegment with that average is longest consecutive occurrences of max.
Now I'll produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1 + n]))
code
max_val = max(a)
best = 0
cur = 0
for x in a:
if x == max_val:
cur += 1
if cur > best:
best = cur
else:
cur = 0
print(best)