Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Remember that a subarray can have length . So every individual is already a valid candidate average.
For any fixed subarray, its average cannot be bigger than the largest element inside that subarray.
If the chosen subarray has values , then for all , where is the max inside it. So .
Dividing by gives . Averaging cannot create a number larger than the biggest ingredient. Math is not a blender.
Since the global maximum element can be taken alone as a length- subarray, the maximum possible average is exactly .
The trap here is thinking we need to inspect every subarray. We don't.
A subarray can have length , so if the array contains some value , we can choose just that one element and get average .
Now let:
Clearly, we can get average by taking the subarray containing only an occurrence of .
So the answer is at least .
Now we prove it can never be bigger.
Take any subarray of length with elements:
Since is the maximum element in the whole array, every element in this subarray satisfies:
Therefore:
Divide both sides by :
That left side is exactly the average of the subarray. So no subarray average can exceed the maximum element of the array.
We already know a length- subarray can achieve that maximum, so the answer is exactly:
That's it. The constraints are tiny, but brute forcing all subarrays would be doing pushups in an elevator. Just print the max.
Algorithm
For each test case:
Complexity
For each test case, we scan the array once.
Time complexity:
Memory complexity:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ans = max(ans, x);
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ans = max(ans, x);
}
cout << ans << '\n';
}
return 0;
}