Problem: 2050B - Transfusion
Language: java

Solution 1 (runtime: 296 ms):
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.Collections.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.Math.*;


// can use min max abs directly

// my code
// methods
/*

    min
    max
    abs
    findEle => check if the element is present in the array or not
    findInd => finds index from the array.

 */

// class name should be changed as required
public class Main {
    private static final int MOD = (int)(1e9 + 7);
    private static final int INF = (int)(1e9);
    private static int[] seive;
    //    private static List<HashSet<Integer>> setSeive;
    // if testcases are set to 1 then only one test will be dont else testcases will be taken as input
    private static int TOTAL_TESTCASES = -1;

    static void solve(Reader r,PrintWriter out,int testcase) throws IOException {
       int n = r.nextInt();
       int[] arr = r.readArr(n);

       long sumo = 0;
       long sume = 0;
       int poso = 0;
       int pose = 0;

       for(int i = 0;i<n;i+=2){
           pose++;
           sume += arr[i];
       }

        for(int i = 1;i<n;i+=2){
            poso++;
            sumo += arr[i];
        }

        if(sume % pose != 0 || sumo % poso != 0 || (sume/pose) != (sumo / poso)){
            out.println("NO");
        }else out.println("YES");
    }




    public static void main(String[] args) throws IOException {
        Reader r = new Reader();
        PrintWriter out = new PrintWriter(System.out);
        if(TOTAL_TESTCASES != 1){
            TOTAL_TESTCASES = r.nextInt();
        }

        int t = 0;
        while(t++ < TOTAL_TESTCASES){
            solve(r,out,t);
        }
        out.flush();
    }

    public static int maxSubArraySum(int[] arr){
        int n = arr.length;
        if(n < 1){
            return 0;
        }
        int ans = arr[0];
        int j = 1;
        int curr = arr[0];
        int mini = min(0,arr[0]);

        while(j < n) {
            curr += arr[j];
            ans = max(ans,curr-mini);
            mini = min(mini,curr);
            j++;
        }
        return ans;
    }

    public static long[] sortDesc(long[] array) {
        Long[] boxedArray = Arrays.stream(array).boxed().toArray(Long[]::new);
        Arrays.sort(boxedArray, (a, b) -> Long.compare(b, a));
        return Arrays.stream(boxedArray).mapToLong(Long::longValue).toArray();
    }
    public static int[] sortDesc(int[] array) {
        Integer[] boxedArray = Arrays.stream(array).boxed().toArray(Integer[]::new);
        Arrays.sort(boxedArray, (a, b) -> b - a);
        return Arrays.stream(boxedArray).mapToInt(Integer::intValue).toArray();
    }

    // sorts treemap by values
    public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortByValues(Map<K, V> map) {
        List<Map.Entry<K, V>> entryList = new ArrayList<>(map.entrySet());
        entryList.sort(Map.Entry.comparingByValue());
        LinkedHashMap<K, V> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : entryList) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }

    static int binarySearch(int[] arr,int ele){
        int n = arr.length;
        int l = 0;
        int r = n-1;

        int ans = -1;

        while(l <= r){
            int mid = l + (r-l)/2;

            // here in if we have the monotonic function this should be designed such that we get the first one as ans in a monotonic array represetation
            if(arr[mid] == ele){
                ans = mid;
                r = mid-1;
            }else{
                l = mid+1;
            }
        }
        return ans;
    }


    // gives first value >= x
    static int lowerBound(int[] arr,int ele){
        int n = arr.length;
        int l = 0;
        int r = n-1;

        int ans = -1;

        while(l <= r){
            int mid = l + (r-l)/2;

            if(arr[mid] >= ele){
                ans = mid;
                r = mid-1;
            }else{
                l = mid+1;
            }
        }
        return ans;
    }

    // gives first element > x
    static int uppperBound(int[] arr,int ele){
        int n = arr.length;
        int l = 0;
        int r = n-1;

        int ans = -1;

        while(l <= r){
            int mid = l + (r-l)/2;

            if(arr[mid] > ele){
                ans = mid;
                r = mid-1;
            }else{
                l = mid+1;
            }
        }
        return ans;
    }

    static int toInt(char c){
        return Integer.parseInt(Character.toString(c));
    }

    // join list of items
    static <T> String joinList(List<T> list, String delimiter) {
        if (list == null || list.isEmpty()) {
            return "";
        }

        return list.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(delimiter));
    }

    // is the number prime
    static boolean isPrime(int n){
        if(n == 1) return false;
        if(n == 2 || n == 3) return true;
        if((n % 2) == 0 || (n % 3) == 0) return false;

        for(int i = 5;i*i<=n;i=i+6){
            if((n%i) == 0 || (n%(i+2)) == 0) return false;
        }
        return true;
    }

    // gcd of the number
    static long gcd(long a, long b){
        if(a==0) return b;
        else return gcd(b%a,a);
    }

    //extended gcd (gets a solution for ax + by = c )

    public static int exGcd(int a, int b, int[] result) {
        if (b == 0) {
            result[0] = 1; // x
            result[1] = 0; // y
            return a; // gcd(a, 0) = a
        }

        int[] tempResult = new int[2];
        int gcd = exGcd(b, a % b, tempResult);
        // Update x and y using results of recursion
        result[0] = tempResult[1];
        result[1] = tempResult[0] - (a / b) * tempResult[1];

        return gcd;
    }
    static long facc(int n){
        if(n <= 2){
            return 2;
        }
        int[] arr = new int[n+1];
        arr[1] = 1;
        arr[2] = 2;
        for(int i = 3;i<=n;i++){
            arr[i] = arr[i-1]*i;
        }

        return arr[n];
    }


    // Fast  power using binary exponiantian
    static int power(int a,int b){
        if(b == 0){
            return 1;
        }
        int half_pow = power(a , b/2);
        if(a % 2 == 0){
            return half_pow * half_pow;
        }else return half_pow * half_pow + a;
    }

    // calculates the lcm of the given number
    static long lcm(long a, long b){
        return (a*b)/gcd(a,b);
    }

    //TC - n logx
    static void calcSeive(int n){
        seive = new int[n+1];
        for(int i = 2;i<=n;i++){
            if(seive[i] != 0) continue; // skipping non prime numbers
            for(int j = 2*i;j<=n;j+=i){
                seive[j]++;
            }
        }
    }

    // till permutations are possible it permutes the passed list else return false;
    private static boolean nextPermutation(List<Integer> perm) {
        int n = perm.size();
        int i = n - 2;
        while (i >= 0 && perm.get(i) >= perm.get(i + 1)) {
            i--;
        }
        if (i < 0)
            return false;

        int j = n - 1;
        while (perm.get(j) <= perm.get(i)) {
            j--;
        }
        Collections.swap(perm, i, j);
        Collections.reverse(perm.subList(i + 1, n));
        return true;
    }

    public static List<Long> getFactors(long number) {
        if (number <= 0) {
            return Collections.emptyList();
        }

        List<Long> factors = new ArrayList<>();

        for(long i = 2; (long) i *i <= number; i++){
            while(number % i == 0){
                factors.add(i);
                number /= i;
            }
        }
        if(number != 1){
            factors.add(number);
        }
        return factors;
    }

    static void debug(int[] a,String... identifier){
        PrintWriter out = new PrintWriter(System.out);
        out.println( ("debug : ") + Arrays.toString(a));
        out.flush();
    }

    // is the number palindrome
    public static boolean isPal(String str1){
        int i = 0;
        int j = str1.length()-1;

        while(i<=j){
            if(str1.charAt(i) != str1.charAt(j)){
                return false;
            }
            i++;
            j--;
        }

        return true;
    }

    static String sort(String str){
        char[] arr = str.toCharArray();
        Arrays.sort(arr);
        return new String(arr);
    }

    static void print(List<Integer> ls ) throws IOException{
        PrintWriter out = new PrintWriter(System.out);
        for(int i : ls){
            out.print(i + " ");
        }
        out.println();
        out.flush();

    }

    static <T> void print(T[] arr ) throws IOException{
        PrintWriter out = new PrintWriter(System.out);

        for(T i : arr){
            out.print(i + " ");
        }
        out.println();
        out.flush();
    }

    static String revStr(String str){
        StringBuilder sb = new StringBuilder("");

        for(int i = str.length()-1;i>=0;i--){
            sb.append(str.charAt(i));
        }

        return sb.toString();
    }

    static class Reader {
        BufferedReader in; StringTokenizer st;
        public Reader() throws IOException {
            in = new BufferedReader(new InputStreamReader(System.in));
            st = new StringTokenizer("");
        }
        public boolean hasNextLine() throws IOException {
            return in.ready();
        }
        public String nextLine() throws IOException {
            st = new StringTokenizer("");
            return in.readLine();
        }
        public String next() throws IOException {
            while (!st.hasMoreTokens())
                st = new StringTokenizer(in.readLine());
            return st.nextToken();
        }
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public int[] readArr(int n) throws IOException{
            int[] arr = new int[n];
            int i  = 0;
            while(i<n){
                arr[i++] = nextInt();
            }
            return arr;
        }
        public long[] readLongArr(int n) throws IOException{
            long[] arr = new long[n];
            int i  = 0;
            while(i<n){
                arr[i++] = nextLong();
            }
            return arr;
        }
    }
    public static void sort(int[] arr) {
        List<Integer> list = new ArrayList<>();
        for (int i : arr) list.add(i);
        Collections.sort(list);
        for (int i = 0; i < arr.length; i++)
            arr[i] = list.get(i);
    }

    public static class Pair<K,V>{
        public K key;
        public V val;
        public Pair(K _key,V _val){
            this.key = _key;
            this.val = _val;
        }

        @Override
        public String toString() {
            return "{" +
                    key +
                    "," + val +
                    '}';
        }
    }
}


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);

    }

}