solution

最大値と最小値を求めてその差。各ケース$O(N)$。

implementation

#include <bits/stdc++.h>
using namespace std;
int main() {
    int t; cin >> t;
    while (t --) {
        int n; cin >> n;
        int min_x = INT_MAX;
        int max_x = INT_MIN;
        while (n --) {
            int x; cin >> x;
            min_x = min(min_x, x);
            max_x = max(max_x, x);
        }
        cout << max_x - min_x << endl;
    }
    return 0;
}