Problem: 2050B - Transfusion
Language: java

Solution 1 (runtime: 639 ms):
/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.*;

public class Main
{
	public static void main(String[] args) {
	    Scanner sc=new Scanner(System.in);
		int o=sc.nextInt();
		sc.nextLine();
// HashMap<Integer,Integer> map=new HashMap<>();

// int[] arr1=new int[o];
		for(int pp=0;pp<o;pp++){
		  int a =sc.nextInt();
		  //int b=sc.nextInt();
		  ////int c=sc.nextInt();
		  ////int d=sc.nextInt();
		  ////int e=sc.nextInt();
		  sc.nextLine();
		  int[] arr=new int[a];
		  long sum=0;
		  long sum1=0;
		  long sum2=0;
		  int odd=(a+1)/2;
		  int even=a/2;
		  for(int i=0;i<a;i++){
		      
		      int xx=sc.nextInt();
		      arr[i]=xx;
		      sum=sum+xx;
		      if(i%2==0){
		      sum1=sum1+xx;}
		      else{
		          sum2=sum2+xx;
		      }
		  }
		  long mod=sum/a;
		  if(sum%a!=0){
		      System.out.println("NO");
		      continue;
		  }
		  if(a==2){
		      if((arr[0]!=arr[1])){
		      System.out.println("NO");
		      continue;
		         }
		      else{
		      System.out.println("YES");
		      continue;
		       }
		  }
		
		  if(a==3){
		      if(arr[1]!=mod){
		      System.out.println("NO");
		      continue;
		      }
		  else{
		      System.out.println("YES");
		      continue;
		       }}
		  if(a>3){
		      if(sum1==(odd*mod)&&(sum2==(even*mod))){
		          System.out.println("YES");
		      continue;
		        }
		      else{
		          System.out.println("NO");
		          continue;
		  
		      }
		  }
		  
		}
		
		
		
}}


Solution 2 (runtime: 718 ms):
#include <iostream>

#include <vector>

#include <algorithm>

#include <cmath>

#include <set>

#include <map>



using namespace std;

using ll = long long;

using vi = vector<int>;

using vvi = vector<vector<int>>;



void solve()

{



    ll n;

    cin >> n;

    vector<ll> a(n);



    ll summ = 0;

    for (ll i = 0; i < n; i++)

    {

        cin >> a[i];

        summ += a[i];

    }



    if (summ % n != 0)

    {

        cout << "NO\n";

        return;

    }



    ll v = summ / n;

    for (ll i = 1; i < n - 1; i++)

    {

        if (a[i - 1] < v)

        {

            ll op = v - a[i - 1];

            if (a[i + 1] < op)

            {

                cout << "NO\n";

                return;

            }

            else

            {

                a[i - 1] = v;

                a[i + 1] -= op;

            }

        }

        else

        {

            ll op = a[i - 1] - v;

            a[i + 1] += op;

            a[i - 1] = v;

        }

    }



    // for(auto num : a) cout << num << " ";

    // cout << "\n";



    if (a[n - 1] == v && a[n - 2] == v)

        cout << "YES\n";

    else

        cout << "NO\n";

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);



    int t = 1;

    cin >> t;

    while (t--)

        solve();

}