A. Beautiful Average

Problem Statement

You are given an array aa of length nn.

Your task is to find the maximum possible average value of any subarray* of the array aa.

Formally, for any indices l,rl, r such that 1lrn1 \le l \le r \le n, define the average of the subarray al,al+1,,ara_l, a_{l+1}, \dots, a_r as the sum of elements divided by the number of elements or:

avg(l,r)=1rl+1i=lrai \text{avg}(l,r) = \frac{1}{r-l+1} \sum_{i=l}^{r} a_i

Output the maximum value of avg(l,r)\text{avg}(l,r) over all choices of l,rl, r.

*A subarray is a contiguous part of an array.

Solution

The problem asks for the maximum average value of any subarray. Let’s consider a subarray of length 1, which consists of a single element aia_i. The average of this subarray is simply aia_i.

Let the maximum element in the array be MM. If we form a subarray of length 1 with this element, the average is MM. Can we get a better average?

Consider any subarray containing the maximum element MM. If we add any other element to this subarray, the new element will be less than or equal to MM. Therefore, the average of the new, larger subarray will not be greater than MM.

For example, if we have a subarray with elements {al,,M,,ar}\{a_l, \dots, M, \dots, a_r\}, the average is airl+1\frac{\sum a_i}{r-l+1}. If we add an element ar+1Ma_{r+1} \le M, the new average will be ai+ar+1rl+2\frac{\sum a_i + a_{r+1}}{r-l+2}. It can be shown that this new average will not exceed the original average if the added element is less than or equal to the original average. Since we know the maximum element is MM, any subarray containing MM and other elements will have an average at most MM.

Therefore, the maximum possible average is achieved by a subarray of length 1, consisting of the maximum element in the array. The problem is reduced to finding the maximum value in the given array.

View Code
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <set>
#include <tuple>
#include <map>
#include <vector>
using namespace std;
#define int long long
// #define mod 1000000007

#define N 500005

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    int ans = INT32_MIN;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ans= max(ans,a[i]);
    }
    cout << ans << endl;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef DEBUG
    freopen("./input.txt", "r", stdin);
#endif
    int t;
    cin >> t;
    while (t--) {
#ifdef DEBUG
        static int test_num = 1;
        cout << "test case: " << test_num++ << endl;
#endif
        solve();
    }
}