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: 202 ms):
import java.io.*;

import java.util.*;



public class Code {



    // Input and Output

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static PrintWriter out = new PrintWriter(System.out);



    // For parsing integers, longs, and strings efficiently

    static StringTokenizer st;



    // Entry point

    public static void main(String[] args) throws IOException {

        int t; // Number of test cases

        t = nextInt(); // Uncomment this line if multiple test cases

        while (t-- > 0) {

            solve(); // Call the solve function for each test case

        }

        out.close(); // Close the output stream

    }







    // Solve function for each test case



    static void solve() throws IOException {

        int n = nextInt();



        int[] arr= new int[n];

        long evensum = 0,oddsum=0;



        for(int i=0;i<n;i++){

            arr[i] = nextInt();



            if(i%2==0){

                evensum+=arr[i];

            }

            else{

                oddsum+= arr[i];

            }

        }

        int evencount = n%2==0?n/2:n/2+1;

        boolean flag = true;



        if((evensum+oddsum)%n == 0){

            if( !(evensum%evencount == 0 && evensum/evencount == (evensum+oddsum)/n &&  oddsum%(n-evencount) == 0 && oddsum/(n-evencount) == (evensum+oddsum)/n) ){

                flag =false;

            }

        }

        else{

            flag = false;

        }



        out.println(flag?"YES":"NO");



    }



    // Utility methods for faster input

    static String next() throws IOException {

        while (st == null || !st.hasMoreTokens()) {

            st = new StringTokenizer(br.readLine());

        }

        return st.nextToken();

    }



    static int nextInt() throws IOException {

        return Integer.parseInt(next());

    }



    static long nextLong() throws IOException {

        return Long.parseLong(next());

    }



    static double nextDouble() throws IOException {

        return Double.parseDouble(next());

    }



    char nextChar() {

        try {

            return (char) br.read();

        } catch (IOException e) {

            e.printStackTrace();

            return '\0'; // Return null character in case of an error

        }

    }



    static String nextLine() throws IOException {

        return br.readLine();

    }



    // Utility functions

    static int gcd(int a, int b) {

        if (b == 0) return a;

        return gcd(b, a % b);

    }



    static int lcm(int a, int b) {

        return a * (b / gcd(a, b));

    }



    static boolean isPrime(int n) {

        if (n <= 1) return false;

        if (n <= 3) return true;

        if (n % 2 == 0 || n % 3 == 0) return false;

        for (int i = 5; i * i <= n; i += 6) {

            if (n % i == 0 || n % (i + 2) == 0) return false;

        }

        return true;

    }



    static long power(long x, long y, long p) {

        long res = 1;

        x = x % p;

        while (y > 0) {

            if ((y & 1) == 1)

                res = (res * x) % p;

            y = y >> 1;

            x = (x * x) % p;

        }

        return res;

    }



    public static void sort(int[] arr)

    {

        //because Arrays.sort() uses quicksort which is dumb

        //Collections.sort() uses merge sort

        ArrayList<Integer> ls = new ArrayList<Integer>();

        for(int x: arr)

            ls.add(x);

        Collections.sort(ls);

        for(int i=0; i < arr.length; i++)

            arr[i] = ls.get(i);

    }



    public static void sort(long[] arr)

    {

        //because Arrays.sort() uses quicksort which is dumb

        //Collections.sort() uses merge sort

        ArrayList<Long> ls = new ArrayList<Long>();

        for(long x: arr)

            ls.add(x);

        Collections.sort(ls);

        for(int i=0; i < arr.length; i++)

            arr[i] = ls.get(i);

    }

}