linear_search_5

 avatar
user_1746670
c_cpp
a month ago
652 B
2
Indexable
Never
#include <bits/stdc++.h>

#define ll long long
#define LIM 105
#define X first
#define Y second
#define EL cout<<"\n"

using namespace std;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int TC; cin >> TC;
	while (TC--) {
		int n; cin >> n;

		int a[n];
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}

		sort (a, a + n);

		if (n <= 1) {
			cout << 0 << endl;
		}
		else if (n == 2) {
			cout << a[1] - a[0] << endl;
		}
		else if (n == 3) {
			cout << a[2] - a[0] << endl;
		}
		else {
			int mx = a[n - 1], mx_2 = a[n - 2], mi = a[0], mi_2 = a[1];
			cout << (mx - mi) + (mx_2 - mi_2) << endl;
		}

	}
	
	return 0;
}
Leave a Comment