Problem: 2050B - Transfusion
Language: c++17 

Code 1:
// Problem: B. Transfusion

// Contest: Codeforces - Codeforces Round 991 (Div. 3)

// URL: https://codeforces.com/contest/2050/problem/B

// Memory Limit: 256 MB

// Time Limit: 2000 ms

// 

// Powered by CP Editor (https://cpeditor.org)



//#pragma GCC optimize("O3")

//#pragma GCC optimize("Ofast")



#include <bits/stdc++.h>

#define Begin ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

#define pii pair<int, int>

#define pdd pair<double, double>

#define lowbit(x) (x & -x)

#define all(x) x.begin(), x.end()



#define Rep(i, x) for(auto &i : x)

#define L(i, x, y) for(int i = x; i <= y; ++ i)

#define R(i, x, y) for(int i = x; i >= y; -- i)

#define rd(x) scanf("%lld", &(x))

#define p_b push_back

#define ft first

#define sd second

using namespace std;

template <class T> using vc = vector<T>;

template <class T> using _pq = priority_queue<T, vector<T>, greater<T>>;

using i64 = long long;

using u64 = unsigned long long;

using u32 = unsigned;

const int N = 2e5 + 10;





































































void solve() {

	int n; cin >> n;

	vc<int> a(n);

	L(i, 0, n - 1) cin >> a[i];

	i64 sum = reduce(a.begin(), a.end());

	if(sum % n) {

		cout << "NO\n";

		return;

	}

	int sd = sum / n;

	//cout << sd << "\n";

	L(i, 0, n - 1) {

		//cout << i << " " << a[i] << "\n";

		if(i >= n - 2 && a[i] != sd) {

			cout << "NO\n";

			return;

		}

		if(a[i] == sd) continue;

		else {

			if((a[i] + a[i + 2]) / 2 != sd || (a[i] + a[i + 2]) % 2) {

				if(a[i] < sd) {

					int dif = sd - a[i];

					a[i] = sd;

					if(a[i + 2] >= dif) a[i + 2] -= dif;

					else {

						cout << "NO\n";

						return;

					}

				}

				else {

					int dif = a[i] - sd;

					a[i] = sd;

					a[i + 2] += dif;

				}

				

			}

			else {

				a[i] = (a[i] + a[i + 2]) / 2;

				a[i + 2] = a[i];

			}

			

		}

	}

	//L()

	cout << "YES\n";

}





int main() {

	Begin;

	int _; _ = 1;

	cin >> _;

	while(_--) solve();

	return 0;

}

Code 2:
// Problem: B. Transfusion

// Contest: Codeforces - Codeforces Round 991 (Div. 3)

// URL: https://codeforces.com/contest/2050/problem/B

// Memory Limit: 256 MB

// Time Limit: 2000 ms

// 

// Powered by CP Editor (https://cpeditor.org)



//#pragma GCC optimize("O3")

//#pragma GCC optimize("Ofast")



#include <bits/stdc++.h>

#define Begin ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

#define pii pair<int, int>

#define pdd pair<double, double>

#define lowbit(x) (x & -x)

#define all(x) x.begin(), x.end()



#define Rep(i, x) for(auto &i : x)

#define L(i, x, y) for(int i = x; i <= y; ++ i)

#define R(i, x, y) for(int i = x; i >= y; -- i)

#define rd(x) scanf("%lld", &(x))

#define p_b push_back

#define ft first

#define sd second

using namespace std;

template <class T> using vc = vector<T>;

template <class T> using _pq = priority_queue<T, vector<T>, greater<T>>;

using i64 = long long;

using u64 = unsigned long long;

using u32 = unsigned;

const int N = 2e5 + 10;





































































void solve() {

	int n; cin >> n;

	vc<int> a(n);

	L(i, 0, n - 1) cin >> a[i];

	i64 sum = reduce(a.begin(), a.end());

	if(sum % n) {

		cout << "NO\n";

		return;

	}

	int sd = sum / n;

	//cout << sd << "\n";

	L(i, 0, n - 1) {

		//cout << i << " " << a[i] << "\n";

		if(i >= n - 2 && a[i] != sd) {

			cout << "NO\n";

			return;

		}

		if(a[i] == sd) continue;

		else {

			if((a[i] + a[i + 2]) / 2 != sd || (a[i] + a[i + 2]) % 2) {

				int dif = a[i] - sd;

				a[i + 2] += dif;

				continue;

			}

			else {

				a[i] = (a[i] + a[i + 2]) / 2;

				a[i + 2] = a[i];

			}

			

		}

	}

	cout << "YES\n";

}





int main() {

	Begin;

	int _; _ = 1;

	cin >> _;

	while(_--) solve();

	return 0;

}

Answer: 1