#include <iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--){
        int n;
        cin >> n;
        int a[n];
        int s = 0;
        for (int i=0; i<n; i++){
            cin >> a[i];
            s += a[i];
        }
        if (s%n){
            cout << "NO" << endl;
            continue;
        }
        s /= n;
        string ans = "YES";
        for (int i=1; i<n-1; i+=2){
            int f = s-a[i-1];
            a[i-1] = s;
            a[i+1] -= f;
        }
        for (int i=2; i<n-1; i+=2){
            int f = s-a[i-1];
            a[i-1] = s;
            a[i+1] -= f;
        }
        for (int i=0; i<n; i++){
            if (a[i]!=s){
                ans = "NO";
            }
        }
        cout << ans << endl;
    }
    return 0;
}

#include <iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--){
        int n;
        cin >> n;
        long long int a[n];
        long long int s = 0;
        for (int i=0; i<n; i++){
            cin >> a[i];
            s += a[i];
        }
        if (s%n){
            cout << "NO" << endl;
            continue;
        }
        s /= n;
        string ans = "YES";
        for (int i=1; i<n-1; i+=2){
            int f = s-a[i-1];
            a[i-1] = s;
            a[i+1] -= f;
        }
        for (int i=2; i<n-1; i+=2){
            int f = s-a[i-1];
            a[i-1] = s;
            a[i+1] -= f;
        }
        for (int i=0; i<n; i++){
            if (a[i]!=s){
                ans = "NO";
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int main()

{

	int t;

	cin >> t;

	while (t--)

	{

		int n;

		cin >> n;

		double ji = 0;

		int cnt_ji = 0;

		double ou = 0;

		int cnt_ou = 0;

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

		{

			int now;

			cin >> now;

			if (i % 2 == 0)

			{

				ji += now;

				cnt_ji++;

			}

			else

			{

				ou += now;

				cnt_ou++;

			}

		}

		double a = ou / cnt_ou;

		double b = ji / cnt_ji;

		int A = a;

		int B = b;

		if (a == b&&a-A==0&&b-B==0)

		{

			cout << "YES" << endl;

		}

		else

		{

			cout << "NO" << endl;

		}

	}

}

#include <bits/stdc++.h>

using namespace std;

int main()

{

	int t;

	cin >> t;

	while (t--)

	{

		int n;

		cin >> n;

		double ji = 0;

		int cnt_ji = 0;

		double ou = 0;

		int cnt_ou = 0;

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

		{

			int now;

			cin >> now;

			if (i % 2 == 0)

			{

				ji += now;

			}

			else

			{

				ou += now;

			}

		}

		if (n % 2 == 0)

		{

			cnt_ou = n / 2;

			cnt_ji = n / 2;

		}

		else

		{

			cnt_ou = n / 2;

			cnt_ji = n / 2+1;

		}

		double a = ou / cnt_ou;

		double b = ji / cnt_ji;

		int A = a;

		int B = b;

		if (a == b&&a-A==0&&b-B==0)

		{

			cout << "YES" << endl;

		}

		else

		{

			cout << "NO" << endl;

		}

	}

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

void solve() {

    int n; cin>> n;

    int sumodd =0, sumeven = 0;

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

      int u; cin>> u;

      if(i&1){

        sumodd += u;

      }

      else sumeven += u;

    }

    int oddsize = n/2 + (n&1);

    int evensize = n/2;

    if((sumodd%evensize) != 0 || (sumeven% oddsize) != 0  ) {

      cout<< "NO"<< endl;

    return;

    }

    if((sumodd/evensize) == (sumeven/oddsize)){

      cout<<"YES"<< endl;

    }

      else cout<<"NO"<< endl;

}

int main() {

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

void solve() {

    int n; cin>> n;

    long long int sumodd =0, sumeven = 0;

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

      int u; cin>> u;

      if(i&1){

        sumodd += u;

      }

      else sumeven += u;

    }

    int oddsize = n/2 + (n&1);

    int evensize = n/2;

    if((sumodd%evensize) != 0 || (sumeven% oddsize) != 0  ) {

      cout<< "NO"<< endl;

    return;

    }

    if((sumodd/evensize) == (sumeven/oddsize)){

      cout<<"YES"<< endl;

    }

      else cout<<"NO"<< endl;

}

int main() {

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

Answer: 2


#include<bits/stdc++.h>

using namespace std;

void solve(){

	int n;

	cin>>n;

	vector<int>a(n);

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

		cin>>a[i];

	}

	bool flag=true;

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

		if(a[i]==a[i-1]){

			flag=false;

			break;

		}

	}

if(flag){

	cout<<"yes"<<endl;

}

else{

	cout<<"no"<<endl;

}

}

int main(){

	int t;

	cin>>t;

	while(t--){

		solve();

	}

	return 0;

}

#include<bits/stdc++.h>

using namespace std;

void solve(){

	int n;

	cin>>n;

	vector<int>a(n);

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

		cin>>a[i];

	}

	bool flag=true;

	if(a[0]==a[n-1]){

		flag=false;

	}

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

		if(a[i]==a[i+1]){

			flag=false;

			break;

		}

	}

	if(flag){

		cout<<"yes"<<endl;

	}

	else{

		cout<<"no"<<endl;

	}

}

int main(){

	int t;

	cin>>t;

	while(t--){

		solve();

	}

	return 0;

}

Answer: 2


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<ll>vec(n);
        ll sum=0,d,b;

        for(int i=0; i<n; i++)
        {
            cin>>vec[i];
            sum+=vec[i];
        }
        if(sum%n!=0)
            cout<<"NO"<<endl;
        else
        {
            d=sum/n;
            bool pos=true;
            for(int a=n-2; a>=0; --a)
            {
                if(vec[a+1]>d)
                {
                    b=vec[a+1]-d;
                    vec[a+1]=d;
                    vec[a-1]+=b;
                }
                else if(vec[a+1]<d)
                {
                    b=d-vec[a+1];
                    vec[a+1]=d;
                    vec[a-1]-=b;
                }

            }
            for(int i=1; i<n; i++)
            {
                if(vec[0]!=vec[i])
                {
                    pos=false;
                    break;
                }
            }
            if(pos) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
}

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<ll>vec(n);
        ll sum=0,d,b;

        for(int i=0; i<n; i++)
        {
            cin>>vec[i];
            sum+=vec[i];
        }
        if(sum%n!=0)
            cout<<"NO"<<endl;
        else
        {
            d=sum/n;
            bool pos=true;
            for(int a=n-2; a>0; --a)
            {
                if(vec[a+1]>d)
                {
                    b=vec[a+1]-d;
                    vec[a+1]=d;
                    vec[a-1]+=b;
                }
                else if(vec[a+1]<d)
                {
                    b=d-vec[a+1];
                    vec[a+1]=d;
                    vec[a-1]-=b;
                }

            }
            for(int i=1; i<n; i++)
            {
                if(vec[0]!=vec[i])
                {
                    pos=false;
                    break;

                }
            }
            if(pos) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
}

Answer: 2


#include <iostream>

#include<vector>

using namespace std;

int main() {

    int t;

    cin>>t;

    while(t--){

        int n;

        cin>>n;

        vector<int> arr(n);

        int seven=0;

        int sodd=0;

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

            cin>>arr[i];

            if(i%2){

                sodd+=arr[i];

            }

            else{

                seven+=arr[i];

            }

        }

        if(seven% (n-n/2) !=0  && sodd%n/2!=0){

            cout<<"NO"<<endl;

        }

        else if(seven/(n-n/2) == sodd/(n/2)){

            cout<<"YES"<<endl;

        }

        else{

            cout<<"NO"<<endl;

        }

    }

    return 0;

}

#include <iostream>

#include<vector>

using namespace std;

int main() {

    int t;

    cin>>t;

    while(t--){

        int n;

        cin>>n;

        vector<int> arr(n);

        long long seven=0;

        long long sodd=0;

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

            cin>>arr[i];

            if((i+1)%2==1){

                sodd+=arr[i];

            }

            else{

                seven+=arr[i];

            }

        }

        if(seven% (n/2) != 0  ||  sodd%(n-n/2)!=0){

            cout<<"NO"<<endl;

        }

        else if(seven/(n/2) == sodd/(n-n/2)){

            cout<<"YES"<<endl;

        }

        else{

            cout<<"NO"<<endl;

        }

    }

    return 0;

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int main(){

    int t;

    cin>>t;

    while(t--){

        long long n;

        cin>>n;

        long long arr[n];

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

        long long sum1 = 0, sum2 =0 ,odd = 0,even = 0;

        for(long long i =0; i<n ; i+=2){

            sum1 = sum1 + arr[i];

            odd++;

        } //odd sum

        for(long long i =1; i<n; i+=2){

            sum2 = sum2 + arr[i];

            even++;

        }//even sum

        if((sum1%odd==0)&&(sum2%even==0)&&(sum1/odd) == (sum2/even)){cout<<"YES"<<endl;}

        else{cout<<"NO"<<endl;}

    }

}

#include <bits/stdc++.h>

using namespace std;

int main(){

    int t;

    cin>>t;

    while(t--){

        int n;

        cin>>n;

        int arr[n];

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

        int sum1 = 0, sum2 =0 ,odd = 0,even = 0;

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

            sum1 = sum1 + arr[i];

            odd++;

        } //odd sum

        for(int i =1; i<n; i+=2){

            sum2 = sum2 + arr[i];

            even++;

        }//even sum

        if((sum1%odd==0)&&(sum2%even==0)&&sum1/odd == sum2/even){cout<<"YES"<<endl;}

        else{cout<<"NO"<<endl;}

    }

}

Answer: 1


#include<iostream>

int main(){
    int t;
    std::cin>>t;
    while(t--){
        int n;
        std::cin>>n;
        int arr[n];
        for(int j=0;j<n;j++){
            std::cin>>arr[j];
        }
        long long odd=0;
        long long even=0;
        for(int i=0;i<n;i++){
            if(i%2==0) even+=arr[i];
            else odd+=arr[i];
        }
        long oddPlace=n/2;
        long evenPlace=(n+1)/2;

        if(odd%oddPlace==0 && even%evenPlace==0 && odd/oddPlace==even/evenPlace) std::cout<<"Yes"<<std::endl;

        else std::cout<<"No"<<std::endl;
    }

    return 0;
}

#include<iostream>

int main(){
    int t;
    std::cin>>t;
    while(t--){
        int n;
        std::cin>>n;
        int arr[n];
        for(int j=0;j<n;j++){
            std::cin>>arr[j];
        }
        int odd=0;
        int even=0;
        for(int i=0;i<n;i++){
            if(i%2==0) even+=arr[i];
            else odd+=arr[i];
        }
        int oddPlace=n/2;
        int evenPlace=(n+1)/2;

        if(odd%oddPlace==0 && even%evenPlace==0 && odd/oddPlace==even/evenPlace) std::cout<<"Yes"<<std::endl;

        else std::cout<<"No"<<std::endl;
    }

    return 0;
}

Answer: 1


#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
int n;
const int maxn=2e5+5;
int a[maxn];
signed main(){
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        int ans=0,sum=0;
        for(int i=1;i<=n;i+=2)
            ans+=a[i];
        for(int i=2;i<=n;i+=2)
            sum+=a[i];
        if(n%2==0){
            if(ans%(n/2)!=0||sum%(n/2)!=0) cout<<"NO"<<endl;
            else{
                if(ans==sum) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
        }else{
            if(ans%(n/2+1)!=0||sum%(n/2)!=0) cout<<"NO"<<endl;
            else{
                if(ans/(n/2+1)==sum/(n/2)) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
        }
    }
    return 0;
}

Answer: 1


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

       int sumo = 0;
       int 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 +
                    '}';
        }
    }
}

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 +
                    '}';
        }
    }
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;

    while (t--)
    {
        int n;
        cin >> n;
        vector<int> a(n);
        float sum_even = 0, sum_odd = 0, count_even = 0, count_odd = 0;

        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (i % 2 == 0)
            {
                sum_even += a[i];
                count_even++;
            }
            else
            {
                sum_odd += a[i];
                count_odd++;
            }
        }

        float avg_even = sum_even / count_even;
        float avg_odd = sum_odd / count_odd;

        if (fmod(avg_even, 2) != 0 || fmod(avg_odd, 2) != 0)
        {
            cout << "NO" << endl;
        }
        else if (count_even == 0 || count_odd == 0 || avg_even != avg_odd)
        {
            cout << "NO" << endl;
        }
        else
        {
            cout << "YES" << endl;
        }
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

void solve(int t) {
    while (t--) {
        int n;
        cin >> n;
        vector<int> v(n);
        long long oddSum = 0, evenSum = 0, oddCount = 0, evenCount = 0;

        for (int i = 0; i < n; i++) {
            cin >> v[i];
            if (i % 2 == 0) {
                oddSum += v[i];
                oddCount++;
            } else {
                evenSum += v[i];
                evenCount++;
            }
        }

        bool isValid = false;

        if (oddSum % oddCount == 0 && evenSum % evenCount == 0) {
            if (oddSum / oddCount == evenSum / evenCount) {
                isValid = true;
            }
        }

        cout << (isValid ? "YES" : "NO") << endl;
    }
}

int main() {
    int t;
    cin >> t;
    solve(t);
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;

typedef vector<long long> vll;

typedef vector<pair<long long, long long>> vpair;

typedef long long ll;

typedef long double lld;

typedef pair<long long, long long> pll;

typedef vector<vector<int>> vvi;

typedef vector<vector<long long>> vvl;

#define f(i, n) for(int i=0; i<n; i++)

#define pb push_back

#define min3(a,b,c) min(a,min(b,c))

#define max3(a,b,c) max(a,max(b,c))

#define nl "\n"

#define ss second

#define ff first

#define mod 1000000007

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

void solve() {

    int n;

    cin >> n;

    vll a(n);

    f(i, n) cin >> a[i];

    for(int i=1; i<n-1; i++) {

        int diff=a[i+1]-a[i-1];

        if(diff>0) {

            a[i-1]+=diff/2;

            a[i+1]-=diff/2;

        }

        else {

            a[i-1]-=(abs(diff)+1)/2;

            a[i+1]+=(abs(diff)+1)/2;

        }

    }

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

        if(a[i]!=a[i+1]) {

            cout << "No\n";

            return;

        }

    }

    cout << "Yes\n";

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    ll t=1;

    cin>>t;

    while(t--) {

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;

typedef vector<long long> vll;

typedef vector<pair<long long, long long>> vpair;

typedef long long ll;

typedef long double lld;

typedef pair<long long, long long> pll;

typedef vector<vector<int>> vvi;

typedef vector<vector<long long>> vvl;

#define f(i, n) for(int i=0; i<n; i++)

#define pb push_back

#define min3(a,b,c) min(a,min(b,c))

#define max3(a,b,c) max(a,max(b,c))

#define nl "\n"

#define ss second

#define ff first

#define mod 1000000007

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

void solve() {

    int n;

    cin >> n;

    ll odds=0, evens=0, oddSum=0, evenSum=0;

    f(i, n) {

        ll x;

        cin >> x;

        if(i&1) {

            odds++;

            oddSum+=x;

        }

        else {

            evens++;

            evenSum+=x;

        }

    }

    if(oddSum%odds==0 && evenSum%evens==0 && oddSum/odds==evenSum/evens) {

        cout << "Yes\n";

    }

    else {

        cout << "No\n";

    }

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    ll t=1;

    cin>>t;

    while(t--) {

        solve();

    }

    return 0;

}

Answer: 2


#include <bits/stdc++.h>
    #include <ctime>
    #include <chrono>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    #define cy() cout << "YES" << '\n'
    #define cn() cout << "NO" << '\n'
    const double pi=    3.14159265358979323846;

    // Function to check if an integer is prime or not
    bool isprime(ll n){
        if(n<2){
            return false;
        }
        for(ll i = 2;i<=sqrt(n);i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    bool pointscompare(pair<string,int> &a ,pair<string,int>  &b){
        return a.second>b.second;
    }
    bool goaldiffcompare(pair<string,int> &a , pair<string,int> &b){
        return a.second>b.second;
    }
    bool cmp(pair<string, int>& a,
        pair<string, int>& b)
    {
        return a.second < b.second;
    }
    bool isSorted(const std::vector<int>& arr) {
    for (size_t i = 1; i < arr.size(); ++i) {
        if (arr[i] < arr[i - 1]) {
            return false;
        }
        }
        return true;
    }

    void solve(){
        //     ...............................
        //     >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        string s;
        cin >> s;
        int sum = 0;
        int two = 0;
        int three = 0;

        for(int i = 0;i<s.size();i++){
            sum += s[i] - '0';
            if(s[i]=='2'){
                two+=1;
            }
            if(s[i]=='3'){
                three+=1;
            }
        }
        set<int> v;
        if(sum%9!=0){

        for(int i = 0;i<=min(8,two);i++){
            for(int j = 0;j<=min(8,three);j++){
                v.insert(2*i + 6*j);
            }
        }
        for(auto x : v){
            // cout << x.first.first << " " << x.first.second << " " << x.second << ' ' << sum%9 << '\n';
        }
        bool p =  false;
        for(auto x : v){
            if((sum+x)%9==0){
                cout << "YES" << '\n';
                p = true;
                break;
            }
        }
        if(!p){
            cout << "NO" << '\n';
        }
        }else{
            cout << "YES" << '\n';
        }
    }
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        std::cout.tie(0);
        // freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
        int tt;
        cin >> tt;
        while(tt--){
            using std::chrono::high_resolution_clock;
            using std::chrono::duration_cast;
            using std::chrono::duration;
            using std::chrono::milliseconds;
            auto t1 = high_resolution_clock::now();
           solve();
            auto t2 = high_resolution_clock::now();
            auto ms_int = duration_cast<milliseconds>(t2 - t1);
            duration<double, std::milli> ms_double = t2 - t1;
            // std::cout << ms_double.count() << "ms\n";
        }
        return 0;
    }

#include <bits/stdc++.h>
    #include <ctime>
    #include <chrono>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    #define cy() cout << "YES" << '\n'
    #define cn() cout << "NO" << '\n'
    const double pi=    3.14159265358979323846;

    // Function to check if an integer is prime or not
    bool isprime(ll n){
        if(n<2){
            return false;
        }
        for(ll i = 2;i<=sqrt(n);i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    bool pointscompare(pair<string,int> &a ,pair<string,int>  &b){
        return a.second>b.second;
    }
    bool goaldiffcompare(pair<string,int> &a , pair<string,int> &b){
        return a.second>b.second;
    }
    bool cmp(pair<string, int>& a,
        pair<string, int>& b)
    {
        return a.second < b.second;
    }
    bool isSorted(const std::vector<int>& arr) {
    for (size_t i = 1; i < arr.size(); ++i) {
        if (arr[i] < arr[i - 1]) {
            return false;
        }
        }
        return true;
    }

    void solve(){
        //     ...............................
        //     >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        string s;
        cin >> s;
        int sum = 0;
        int two = 0;
        int three = 0;

        for(int i = 0;i<s.size();i++){
            sum += s[i] - '0';
            if(s[i]=='2'){
                two+=1;
            }
            if(s[i]=='3'){
                three+=1;
            }
        }
        set<int> v;
        for(int i = 0;i<=two;i++){
            for(int j = 0;j<=three;j++){
                v.insert(2*i + 6*j);
            }
        }
        for(auto x : v){
            // cout << x.first.first << " " << x.first.second << " " << x.second << ' ' << sum%9 << '\n';
        }
        bool p =  false;
        for(auto x : v){
            if((sum+x)%9==0){
                cout << "YES" << '\n';
                p = true;
                break;
            }
        }
        if(!p){
            cout << "NO" << '\n';
        }

    }
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        std::cout.tie(0);
        // freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
        int tt;
        cin >> tt;
        while(tt--){
            using std::chrono::high_resolution_clock;
            using std::chrono::duration_cast;
            using std::chrono::duration;
            using std::chrono::milliseconds;
            auto t1 = high_resolution_clock::now();
           solve();
            auto t2 = high_resolution_clock::now();
            auto ms_int = duration_cast<milliseconds>(t2 - t1);
            duration<double, std::milli> ms_double = t2 - t1;
            // std::cout << ms_double.count() << "ms\n";
        }
        return 0;
    }

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define fr(x) for (int i = 1; i <= x; i++)
#define debug(x) cout << x << "\n"
#define MAXN 300006
#define MOD 998244353
typedef long long int ll;

int arr[MAXN];

ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1;
    ll gcd = gcdExtended(b%a, a, &x1, &y1);
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll modInverse(ll b, ll m)
{
    ll x, y;
    ll g = gcdExtended(b, m, &x, &y);
    return (x%m + m) % m;
}

ll divisionModular(ll a, ll b, ll m)
{
    a = a % m;
    ll inv = modInverse(b, m);
    ll resp = (inv * a) % m;
    return resp;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t;
    cin >> t;

    while(t--){
        string n;
        int sum = 0, dos = 0, seis = 0;
        cin >> n;
        for (char num: n ){
            sum += (num - '0');
            if(num == '2') dos++;
            if(num == '3') seis++;
        }

        bool pudo = false;
        if(sum % 9 == 0) pudo = true;

        for (int i = 1; i <= 100000; i++){
            int posible  = sum + (2 * i);
            if(posible % 9 == 0){
                int seisNeed = i / 3;
                int dosNeed = i - (seisNeed * 3);
                if(seisNeed > seis){
                    seisNeed -= seis;
                    dosNeed = dosNeed + (seisNeed * 3);
                }
                if(dos >= dosNeed){
                    pudo = true;
                    break;
                }
            }
        }

        if(pudo) cout << "YES";
        else cout << "NO";

        cout << "\n";
    }

    return 0;
}

/*
1
6 8
1 4 2 7 3 5
7 6 5 6 3 3
2 5
1 6
1 5
1 5
1 5
2 3
2 3
1 6

2 3 3 3 4 4 4 4 4 4 5 6 7 7 7 7 9
*/

#include <bits/stdc++.h>
using namespace std;

#define fr(x) for (int i = 1; i <= x; i++)
#define debug(x) cout << x << "\n"
#define MAXN 300006
#define MOD 998244353
typedef long long int ll;

int arr[MAXN];

ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1;
    ll gcd = gcdExtended(b%a, a, &x1, &y1);
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll modInverse(ll b, ll m)
{
    ll x, y;
    ll g = gcdExtended(b, m, &x, &y);
    return (x%m + m) % m;
}

ll divisionModular(ll a, ll b, ll m)
{
    a = a % m;
    ll inv = modInverse(b, m);
    ll resp = (inv * a) % m;
    return resp;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t;
    cin >> t;

    while(t--){
        string n;
        int sum = 0, dos = 0, seis = 0;
        cin >> n;
        for (char num: n ){
            sum += (num - '0');
            if(num == '2') dos++;
            if(num == '3') seis++;
        }

        if(sum % 9 == 0){
            cout << "YES";
        }else{
            int falta = 9 - (sum % 9);
            bool pudo = false;
            while(dos >= 1 || seis >= 1){
                if(dos == 0){
                    while(seis >= 1){
                        sum += 6;
                        seis--;
                        if(sum % 9 == 0){
                            seis = 0;
                            pudo = true;
                            break;
                        }
                    }
                }else if(seis == 0){
                    while(dos >= 1){
                        sum += 2;
                        dos--;
                        if(sum % 9 == 0){
                            pudo = true;
                            dos = 0;
                            break;
                        }
                    }
                }else{
                    if(falta >= 6){
                        sum += 6;
                        falta -= 6;
                        seis--;
                    }
                    else{
                        sum += 2;
                        falta -= 2;
                        dos--;
                    }
                    if(falta == 0){
                        pudo = true;
                        break;
                    }else{
                        falta = 9 - (sum % 9);
                    }
                }

            }
            if(pudo) cout << "YES";
            else cout << "NO";
        }

        cout << "\n";
    }

    return 0;
}

/*
1
6 8
1 4 2 7 3 5
7 6 5 6 3 3
2 5
1 6
1 5
1 5
1 5
2 3
2 3
1 6

2 3 3 3 4 4 4 4 4 4 5 6 7 7 7 7 9
*/

Answer: 1


#include<bits/stdc++.h>

using namespace std;

long long get_sum(long long sum){
    while(sum > 9){
        long long n = sum;
        sum = 0;
        while(n > 0){
            sum += n%10;
            n /= 10;
        }
    }
    return sum;
}

void solve(string s){
    long long arr[s.size()];
    long long sum = 0,two = 0,three = 0;
    for(int i = 0;i < s.size();i++){
        arr[i] = s[i] - '0';
        sum += arr[i];
        if(arr[i] == 2)two++;
        else if(arr[i] == 3)three++;
    }
    sum = get_sum(sum);
    for(int i = 0;i < min(10,two + 1);i++){
        for(int j = 0;j < min(10,three + 1);j++){
            if((sum + i*2 + j*3)%9 == 0){
                cout << "YES\n";
                return;
            }
        }
    }
    cout << "NO\n";
    return;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        solve(s);
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

long long get_sum(long long sum){
    while(sum > 9){
        long long n = sum;
        sum = 0;
        while(n > 0){
            sum += n%10;
            n /= 10;
        }
    }
    return sum;
}

void solve(string s){
    long long arr[s.size()];
    long long sum = 0;
    int two = 0,three = 0;
    for(int i = 0;i < s.size();i++){
        arr[i] = s[i] - '0';
        sum += arr[i];
        if(arr[i] == 2)two++;
        else if(arr[i] == 3)three++;
    }
    sum = get_sum(sum);
    for(int i = 0;i < min(10,two + 1);i++){
        for(int j = 0;j < min(10,three + 1);j++){
            if((sum + i*2 + j*6)%9 == 0){
                cout << "YES\n";
                return;
            }
        }
    }
    cout << "NO\n";
    return;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        solve(s);
    }
    return 0;
}

Answer: 2


testCases = int(input())
for i in range(testCases):
    two = 0
    three = 0
    total = 0
    number = input()
    for j in number:

        if j == "2":
            two += 1
        elif j == "3":
            three += 1
        total += int(j)
    total = total % 9
    if total == 0:
        print("YES")
    elif total == 3 and three >= 1 or total == 6 and three >= 2:
        print("YES")
    elif (9-total) % 2 == 0 and int((9-total) / 2) <= two or (18-total) % 2 == 0 and int((18-total) / 2) <= two:
        print("YES")
    elif three >= 2:
        total += 3
        total = total % 9
        if (9-total) % 2 == 0 and int((9-total) / 2) <= two or (18-total) % 2 == 0 and int((18-total) / 2) <= two:
            print("YES")
        else:
            print("NO")
    elif three >= 1:
        total += 6
        total = total % 9
        if (9-total) % 2 == 0 and int((9-total) / 2) <= two or (18-total) % 2 == 0 and int((18-total) / 2) <= two:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

testCases = int(input())
for i in range(testCases):
    two = 0
    three = 0
    total = 0
    number = input()
    for j in number:

        if j == "2":
            two += 1
        elif j == "3":
            three += 1
        total += int(j)
    total = total % 9
    if total == 0:
        print("YES")
    elif total == 3 and three >= 1 or total == 6 and three >= 2:
        print("YES")
    elif (9-total) % 2 == 0 and int((9-total) / 2) <= two or (18-total) % 2 == 0 and int((18-total) / 2) <= two:
        print("YES")
    elif three >= 2:
        total += 3
        total = total % 9
        if (9-total) % 2 == 0 and int((9-total) / 2) <= two or (18-total) % 2 == 0 and int((18-total) / 2) <= two:
            print("YES")
        else:
            total += 3
            total = total % 9
            if (9-total) % 2 == 0 and int((9-total) / 2) <= two or (18-total) % 2 == 0 and int((18-total) / 2) <= two:
                    print("YES")
            else:
                print("NO")
    elif three >= 1:
        total += 6
        total = total % 9
        if (9-total) % 2 == 0 and int((9-total) / 2) <= two or (18-total) % 2 == 0 and int((18-total) / 2) <= two:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int main(){

    int t;

    cin >> t;

    while(t--){

        string s;

        cin >> s;

        int n = s.length(), r = 0, c2 = 0, c3 = 0;

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

            c2 += s[i] == '2';

            c3 += s[i] == '3';

            r = (r + s[i] - '0') % 9;

        }

        cout << r << " " << c2 << " " << c3 << endl;

        bool flag = false;

        for(int i = 0; i <= min(c2, 8); i++){

            for(int j = 0; j <= min(2, c3); j++)    if((r + i + i + j + j + j) % 9 == 0){

                flag = true;

                break;

            }

            if(flag)    break;

        }

        cout << (flag? "YES": "NO") << endl;

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

int main(){

    int t;

    cin >> t;

    while(t--){

        string s;

        cin >> s;

        int n = s.length(), r = 0, c2 = 0, c3 = 0;

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

            c2 += s[i] == '2';

            c3 += s[i] == '3';

            r = (r + s[i] - '0') % 9;

        }

        bool flag = false;

        for(int i = 0; i <= min(c2, 8); i++){

            for(int j = 0; j <= min(2, c3); j++)    if((r + i + i + j * 6) % 9 == 0){

                flag = true;

                break;

            }

            if(flag)    break;

        }

        cout << (flag? "YES": "NO") << endl;

    }

    return 0;

}

Answer: 2


#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define F first
#define S second
#define M "MUHA"
#define all(x) x.begin(),x.end()
#define ssd ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn = 1e5+5,N=20,mod = 1e9+7;
void solve(){
    int n;
    cin >>n;
    int cal2=0,cal3=0;
    int cal=0;
    string s = to_string(n);
    for(int i=0;i<s.size();i++){
        cal+=s[i]-'0';
        if(s[i]=='2')cal2++;
        if(s[i]=='3')cal3++;
    }
    if(cal%9==0){
        cout<<"YES"<<'\n';
        return;
    }
    int c2=10,c3=10;
    c2 = min(c2,cal2);c3 = min(c3, cal3);
    for(int i=0;i<=c3;i++){
        for(int j=0;j<=c2;j++){
            if((i*6+j*2+cal)%9==0){
                cout<<"YES"<<'\n';
                return;
            }
        }
    }
    cout<<"NO"<<'\n';
}
signed main(){
    //freopen("lepus.in", "r", stdin);
    //freopen("lepus.out",   "w", stdout);
    ssd
    int t=1;
    cin>>t;
    for(int i=1;i<=t;i++){
        //cout<<"Case "<<i<<":"<<'\n';
        solve();
    }
}

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define F first
#define S second
#define M "MUHA"
#define all(x) x.begin(),x.end()
#define ssd ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn = 1e5+5,N=20,mod = 1e9+7;
void solve(){
    string s;
    cin >>s;
    int cal2=0,cal3=0;
    int cal=0;
    for(int i=0;i<s.size();i++){
        cal+=s[i]-'0';
        if(s[i]=='2')cal2++;
        if(s[i]=='3')cal3++;
    }

    if(cal%9==0){
        cout<<"YES"<<'\n';
        return;
    }
    int c2=10,c3=10;
    c2 = min(c2, cal2);
    c3 = min(c3, cal3);
    for(int i=0;i<=c3;i++){
        for(int j=0;j<=c2;j++){
            if((i*6+j*2+cal)%9==0){
                cout<<"YES"<<'\n';
                return;
            }
        }
    }
    cout<<"NO"<<'\n';
}
signed main(){
    //freopen("lepus.in", "r", stdin);
    //freopen("lepus.out",   "w", stdout);
    ssd
    int t=1;
    cin>>t;
    for(int i=1;i<=t;i++){
        //cout<<"Case "<<i<<":"<<'\n';
        solve();
    }
}

Answer: 2


#include<bits/stdc++.h>

using namespace std;

#define ll long long

void solve(){

    string s;

    cin>>s;

    int c2 = 0, c3 = 0, sum = 0;

    for(int i=0; i<s.size(); i++){

        if(s[i] == '2')c2++;

        else if(s[i] == '3')c3++;

        sum += int(s[i]-'0');

    }

    function<bool()> check = [&]() -> bool {

        int r = (9-sum%9)%9;

        c3 %= 3;

        c2 %= 9;

        if(c2 >= (r*5)%9)return true;

        if(c3 > 0 && c2 >= ((r+3)*5)%9)return true;

        if(c3 > 1 && c2 >= ((r+6)*5)%9)return true;

        return false;

    };

    cout<<(check() ? "YES" : "NO")<<"\n";

}

int main(){

    int t;

    cin>>t;

    while(t--){

        solve();

    }

}

#include<bits/stdc++.h>

using namespace std;

#define ll long long

void solve(){

    string s;

    cin>>s;

    int c2 = 0, c3 = 0, sum = 0;

    for(int i=0; i<s.size(); i++){

        if(s[i] == '2')c2++;

        else if(s[i] == '3')c3++;

        sum += int(s[i]-'0');

    }

    function<bool()> check = [&]() -> bool {

        for(int i=0; i<=min(c2, 9); i++){

            for(int j=0; j<=min(c3, 9); j++){

                if((sum+2*i+6*j) % 9 == 0)return true;

            }

        }

        return false;

    };

    cout<<(check() ? "YES" : "NO")<<"\n";

}

int main(){

    int t;

    cin>>t;

    while(t--){

        solve();

    }

}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define int long long

bool fn(int num_2, int num_6, int sum){

    vector<bool> dp(9,false);
    dp[sum] = true;

    for(int i=0;i<=num_2;i++){
        for(int j=0;j<=num_6;j++){
            if(i==0 && j==0){
                continue;
            }
            int num = sum + 2*i + 6*j;
            num%=9;
            dp[num] = true;
        }
    }

    return dp[0];
}

main(){

    cin.tie(0);
    ios::sync_with_stdio(0);

    int t;

    cin>>t;

    while(t--){
        string s;

        cin>>s;

        int n = s.size();
        int num_2 = 0;
        int num_6 = 0;

        int sum = 0;

        for(int i=0;i<n;i++){
            int num = s[i] - '0';
            sum += num;

            if(num==2){
                num_2++;
            }

            if(num==3){
                num_6++;
            }
        }
        sum%=9;

        if(fn(num_2,num_6,sum)){
            cout<<"YES"<<endl;
        }

        else{
            cout<<"NO"<<endl;
        }

    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long

bool fn(int num_2, int num_6, int sum){

    vector<bool> dp(9,false);
    dp[sum] = true;

    for(int i=0;i<=min((int)18,num_2);i++){
        for(int j=0;j<=min(num_6,(int)3);j++){
            if(i==0 && j==0){
                continue;
            }
            int num = sum + 2*i + 6*j;
            num%=9;
            dp[num] = true;
        }
    }

    return dp[0];
}

main(){

    cin.tie(0);
    ios::sync_with_stdio(0);

    int t;

    cin>>t;

    while(t--){
        string s;

        cin>>s;

        int n = s.size();
        int num_2 = 0;
        int num_6 = 0;

        int sum = 0;

        for(int i=0;i<n;i++){
            int num = s[i] - '0';
            sum += num;

            if(num==2){
                num_2++;
            }

            if(num==3){
                num_6++;
            }
        }
        sum%=9;

        if(fn(num_2,num_6,sum)){
            cout<<"YES"<<endl;
        }

        else{
            cout<<"NO"<<endl;
        }

    }

    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        string n;
        cin >> n;
        vector<int> digits;
        int sum = 0;
        for (char c : n) {
            int digit = c - '0';
            digits.push_back(digit);
            sum += digit;
        }
        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }
        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (int i = 0; i <= count2 && !possible; i++) {
            for (int j = 0; j <= count3 && !possible; j++) {
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
        }
        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;

        vector<int> digits;
        ll sum = 0;

        while (n > 0) {
            ll temp = n % 10;
            digits.push_back(temp);
            sum += temp;
            n /= 10;
        }

        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }

        ll count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (ll i = 0; i <= count2 && !possible; i++) {
            for (ll j = 0; j <= count3 && !possible; j++) {
                ll newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
            if(possible){break;}
        }

        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

Answer: 1


Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve()
{
    /* ll n;
    cin >> n;
    string s = to_string(n); */
    string s;
    cin >> s;
    ll sum = 0LL;
    ll _2 = 0, _3 = 0;
    for (auto it : s)
    {
        sum += it - '0';
        if (it == '2')
            _2++;
        else if (it == '3')
            _3++;
    }
    if (sum % 9 == 0)
    {
        cout << "YES" << endl;
        return;
    }
    else
    {
        for (ll i = 0; i <= _2; i++)
        {
            for (ll j = 0; j <= _3; j++)
            {
                if ((sum + (2 * i) + (6 * j)) % 9 == 0)
                {
                    cout << "YES" << endl;
                    return;
                }
            }
        }
        cout << "NO" << endl;
        return;
    }
}
int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve()
{
    ll n;
    cin >> n;
    string s = to_string(n);
    ll sum = 0LL;
    ll _2 = 0, _3 = 0;
    for (auto it : s)
    {
        sum += it - '0';
        if (it == '2')
            _2++;
        else if (it == '3')
            _3++;
    }
    if (sum % 9 == 0)
    {
        cout << "YES" << endl;
        return;
    }
    else
    {
        for (ll i = 0; i <= _2; i++)
        {
            for (ll j = 0; j <= _3; j++)
            {
                if ((sum + 2 * i + 6 * j) % 9 == 0)
                {
                    cout << "YES" << endl;
                    return;
                }
            }
        }
        cout << "NO" << endl;
        return;
    }
}
int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

Answer: 1


import Control.Monad (forM_)

main :: IO ()
main = do
  t <- readLn :: IO Int
  forM_ [1 .. t] $ \_ -> do
    n <- getLine
    putStrLn (solveStr n)

solveStr inp = if solve inp then "YES" else "NO"

solve :: String -> Bool
solve num =
  let (num2, num3) = (numDigits '2' num, numDigits '3' num)
   in checkPossible num2 num3 (sumDigits num)

checkPossible :: Int -> Int -> Int -> Bool
checkPossible num2 num3 total
  | min num2 num3 < 0 = False
  | total `mod` 9 == 0 = True
  | otherwise = checkPossible (num2 - 1) num3 (total + 2) || checkPossible num2 (num3 - 1) (total + 6)

numDigits :: Char -> [Char] -> Int
numDigits target = length . filter (== target)

sumDigits :: [Char] -> Int
sumDigits str = sum (map convertToInt str)
  where
    convertToInt c = read [c] :: Int

import Control.Monad (forM_)

main :: IO ()
main = do
  t <- readLn :: IO Int
  forM_ [1 .. t] $ \_ -> do
    n <- getLine
    putStrLn (solveStr n)

solveStr inp = if solve inp then "YES" else "NO"

solve :: String -> Bool
solve num =
  let (num2, num3) = (numDigits '2' num, numDigits '3' num)
   in checkPossible num2 num3 num2 num3 (sumDigits num)

checkPossible num2 num3 num2In num3In total
  | num2In - num2 > 9 = False
  | num3In - num3 > 9 = False
  | min num2 num3 < 0 = False
  | total `mod` 9 == 0 = True
  | otherwise = checkPossible (num2 - 1) num3 num2In num3In (total + 2) || checkPossible num2 (num3 - 1) num2In num3In (total + 6)

numDigits :: Char -> [Char] -> Int
numDigits target = length . filter (== target)

sumDigits :: [Char] -> Int
sumDigits str = sum (map convertToInt str)
  where
    convertToInt c = read [c] :: Int

Answer: 2


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int t;

    cin >> t;

    while(t--){

        string s;

        cin >> s;

        ll sum = 0;

        int freq[10] = {0};

        for (char c : s) {

            int d = c - '0';

            sum += d;

            freq[d]++;

        }

       ll p = sum % 9;

        int count2 = freq[2], count3 = freq[3];

        bool possible = false;

        for (int i = 0; i <= count2 && !possible; i++){

            for (int j = 0; j <= count3; j++){

                if ((p + 2 * i + 6 * j) % 9 == 0) {

                    possible = true;

                    break;

                }

            }

        }

        cout << (possible ? "YES" : "NO") << "\n";

    }

    return 0;

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int main() {

	// your code goes here

	long long t;

	cin>>t;

	while(t--){

	    string n;

	    cin>>n;

	    int k=n.size();

	    int a[10]={0};

	    long long sum=0;

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

	        a[n[i]-'0']++;

	        sum+=n[i]-'0';

	    }

	    int flag=0;

	    if(sum%9==0){

	        cout<<"Yes"<<endl;

	        flag=1;

	    }

	    else{

	        for(int i=0;i<=a[2];i++){

	            for(int j=0;j<=a[3];j++){

	                if((sum+i*2+j*6)%9==0){

	                    cout<<"Yes"<<endl;

	                    flag=1;

	                    break;

	                }

	            }

	        }

	    }

	    if(flag==0){

	        cout<<"No"<<endl;

	    }

	}

}

#include <bits/stdc++.h>

using namespace std;

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    long long t;

    cin >> t;

    while(t--){

        string n;

        cin >> n;

        int k = n.size();

        // Use long long for the frequency array as well

        long long a[10] = {0};

        long long sum = 0;

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

            int digit = n[i] - '0';

            a[digit]++;

            sum += digit;

        }

        bool possible = false;

        // Check if sum is already divisible by 9

        if(sum % 9 == 0){

            cout << "Yes" << "\n";

            possible = true;

        }

        else {

            // Try to adjust the sum using available 2's (value 2) and 3's (value 6)

            for(long long i = 0; i <= a[2] && !possible; i++){

                for(long long j = 0; j <= a[3]; j++){

                    if((sum + i * 2 + j * 6) % 9 == 0){

                        cout << "Yes" << "\n";

                        possible = true;

                        break;

                    }

                }

            }

        }

        if(!possible){

            cout << "No" << "\n";

        }

    }

    return 0;

}

Answer: 2


import sys
import math
import collections
import statistics
import bisect

input = sys.stdin.readline

############ ---- Input Functions ---- ############
##---1) inp — For taking integer inputs.
def inp():
    return (int(input()))

##2) inlt — For taking List inputs.
def inlt():
    return (list(map(int, input().split())))

#3) insr — For taking string inputs. Actually it returns a List of Characters,
# instead of a string, which is easier to use in Python, because in Python,
# Strings are Immutable.
def insr():
    s = input()
    return (list(s[:len(s) - 1]))

#4) invr — For taking space seperated integer variable inputs.
def invr():
    return list(map(int, input().split()))

for iii in range(int(input())):
    a = insr()
    sum = 0
    num2 = 0
    num3 = 0
    for i in a:
        sum += int(i)
        if i == '2':
            num2 += 1
        elif i == '3':
            num3 += 1
    if sum % 9 == 0:
        print("yes")
        continue
    res = 0
    for x in range(min(num2+1, 10)):
        if res == 0:
            for y in range(min(num3+1, 10)):
                if (sum + 2*x + 6*y) % 9 == 0:
                    print("yes")
                    res = 1
                    break
        else:
            break
    if res == 0:
        print("no")

import sys
import math
import collections
import statistics
import bisect

input = sys.stdin.readline

############ ---- Input Functions ---- ############
##---1) inp — For taking integer inputs.
def inp():
    return (int(input()))

##2) inlt — For taking List inputs.
def inlt():
    return (list(map(int, input().split())))

#3) insr — For taking string inputs. Actually it returns a List of Characters,
# instead of a string, which is easier to use in Python, because in Python,
# Strings are Immutable.
def insr():
    s = input()
    return (list(s[:len(s) - 1]))

#4) invr — For taking space seperated integer variable inputs.
def invr():
    return list(map(int, input().split()))

for iii in range(int(input())):
    a = inlt()
    sum = 0
    num2 = 0
    num3 = 0
    for i in str(a):
        sum += int(i)
        if i == '2':
            num2 += 1
        elif i == '3':
            num3 += 1
    if sum % 9 == 0:
        print("yes")
        continue
    res = 0
    for x in range(min(num2+1, 10)):
        if res == 0:
            for y in range(min(num3+1, 10)):
                if (sum + 2*x + 6*y) % 9 == 0:
                    print("yes")
                    res = 1
                    break
        else:
            break
    if res == 0:
        print("no")

Answer: 1


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        string n;
        cin >> n;

        int rem = 0;
        int tcount = 0, threecount = 0;

        // Calculate the remainder and count digits '2' and '3'
        for (char c : n) {
            int digit = c - '0';
            rem = (rem + digit) % 9;
            if (digit == 2) tcount++;
            else if (digit == 3) threecount++;
        }

        bool flag = false;

        // Check if adding multiples of 2 and 3 makes (rem) divisible by 9
        for (int i = 0; (i <= 9) && (i <= tcount) && (!flag); i++) {
            for (int j = 0; (j <= 9) && (j <= threecount) && (!flag); j++) {
                if ((rem + (2 * i) + (6 * j)) % 9 == 0) {
                    flag = true;
                }
            }
        }

        cout << (flag ? "YES\n" : "NO\n");
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {

        long long int n;
        cin >> n;
        long long int rem=n%9;
        long long int tcount=0,threecount=0;
        while(n!=0){
            if(n%10 == 2)
            {
                tcount++;
            }
            else if(n%10==3)
            {
                threecount++;
            }
            n = n/10;
        }
        int flag=0;
        for(long long int i=0;(i<=9)&&(i<=tcount)&&(!flag);i++){
            for(long long int j=0;(j<=9)&&(j<=threecount)&&(!flag);j++)
            {
                if((rem+ (2*i) + (6*j))%9==0)
                {
                    flag =1;
                }
            }
        }
        if(flag){
            cout << "YES\n";
        }
        else cout << "NO\n";
    }

    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+5,mod=1e9+7,inf=1e18;
int _t,n,c[5]; string s; bool ans;
signed main(){
    ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>_t;
	while (_t--){
		cin>>s; n=0; c[2]=c[3]=0; ans=0;
		for (int i=0; i<(int)s.size(); i++){
			if (s[i]=='2') c[2]++;
			else if (s[i]=='3') c[3]++;
			else n+=s[i]-'0';
			n%=9; c[2]%=9; c[3]%=9;
		}
		for (int i=0; i<=c[2]; i++)
			for (int j=0; j<=c[3]; j++){
				if ((n+i*4+(c[2]-i)*2+j*3+(c[3]-j)*9)%9==0) ans=1;
			}
		cout<<(ans?"YES\n":"NO\n");
	}
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+5,mod=1e9+7,inf=1e18;
int _t,n,c[5]; string s; bool ans;
signed main(){
    ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>_t;
	while (_t--){
		cin>>s; n=0; c[2]=c[3]=0; ans=0;
		for (int i=0; i<(int)s.size(); i++){
			if (s[i]=='2') c[2]++;
			else if (s[i]=='3') c[3]++;
			n+=s[i]-'0';
		}
		for (int i=0; i<=c[2]; i++)
			for (int j=0; j<=c[3]; j++){
				if ((n+i*2+j*6)%9==0) ans=1;
			}
		cout<<(ans?"YES\n":"NO\n");
	}
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
int main()
{
	//NOI;
	//freopen("road.in","r",stdin);
	//freopen("road.out","w",stdout);
	int T;
	cin>>T;
	while(T--)
	{
		string N;
		cin>>N;
		int n=N.length();
		int sum=0;
		int ssum=0,tsum=0;
		for(int i=0;i<n;i++)
		{
			sum+=N[i]-'0';
			if(N[i]-'0'==2)tsum++;
			else if(N[i]-'0'==3)ssum++;
		}
		sum%=9;
//		cout<<sum;
		if(sum==0){
			cout<<"YES"<<endl;
			continue;
		}
		bool check=0;
		for(int i=0;i<=tsum;i++)
		{
			for(int j=0;j<=ssum;j++)
			{
				if((sum+i*2+j*6)%9==0){

				cout<<"YES";
				check=1;}
			}
		}
		if(check==0)cout<<"NO";
		cout<<endl;
	}
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
	//NOI;
	//freopen("road.in","r",stdin);
	//freopen("road.out","w",stdout);
	long long T;
	cin>>T;
	while(T--)
	{
		string N;
		cin>>N;
		long long n=N.size();
		long long sum=0;
		long long ssum=0,tsum=0;
		for(long long i=0;i<n;i++)
		{
			sum+=N[i]-'0';
			if(N[i]-'0'==2)tsum++;
			else if(N[i]-'0'==3)ssum++;
		}
		sum%=9;
		if(sum==0){
			cout<<"YES"<<endl;
			continue;
		}
		bool check=0;
		for(long long i=0;i<=tsum;i++)
		{
			for(long long j=0;j<=ssum;j++)
			{
				if((sum+i*2+j*6)%9==0){

					cout<<"YES";
					check=1;
					break;
				}

			}if(check==1)break;
		}
		if(check==0)cout<<"NO";
		cout<<endl;
	}
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

    int t;
    cin >> t;
    while(t--)
    {
        string n;
        cin >> n;
        int twocount  = 0;
        int threecount = 0;
        ll sum = 0;
        bool flag = false;
        for(int i =0; i < n.size(); i++)
        {
            if(n[i] == '2')
            twocount++;
            else if(n[i] == '3')
            threecount++;
            sum += n[i] - '0';
        }
        for(int i = 0 ; i < min(10, twocount+1); i++)
        {
            for(int j = 0; j < min(10,threecount+1); j++)
            {
                if((sum + 2 * i + 6 * j) % 9 == 0)
                {
                    flag = true;
                }
            }
        }
        if(flag)
        cout << "YES" << endl;
        else
        cout << "NO" << endl;

    }
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

    int t;
    cin >> t;
    while(t--)
    {
        string n;
        cin >> n;
        int twocount  = 0;
        int threecount = 0;
        ll sum = 0;
        bool flag = false;
        for(int i =0; i < n.size(); i++)
        {
            if(n[i] == '2')
            twocount++;
            else if(n[i] == '3')
            threecount++;
            sum += n[i] - '0';
        }
        for(int i = 0 ; i < min(8, twocount+1); i++)
        {
            for(int j = 0; j < min(8,threecount+1); j++)
            {
                if((sum + 2 * i + 6 * j) % 9 == 0)
                {
                    flag = true;
                }
            }
        }
        if(flag)
        cout << "YES" << endl;
        else
        cout << "NO" << endl;

    }
}

Answer: 1


// Problem: C. Uninteresting Number
// Contest: Codeforces - Codeforces Round 991 (Div. 3)
// URL: https://codeforces.com/problemset/problem/2050/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define int long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define umap unordered_map
#define mii map<int, int>
#define pii pair<int, int>
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vi>
#define vpii vector<pii>
#define ff first
#define ss second
#define rep(i,x,y) for(int i=x; i<y; i++)
#define rrep(i,x,y) for(int i=x; i>=y; i--)
#define yn(f) f?cout<<"YES\n":cout<<"NO\n"
#define syn(f) f?cout<<"Yes\n":cout<<"No\n"
#define print(x) for(auto l:x) cout<<l<<" ";cout<<'\n';

int binexp(int a, int b,int M) {
    if(b==0) return 1;
    int res=binexp(a,b/2,M);
    if(b%2==1) return (((res*res)%M)*(a%M))%M;
    else return (res*res)%M;
}

int M = 1e9 + 7;
// int M = 998244353;

void solve(){
    int twos = 0;
    int threes = 0;
    string n;
    cin >> n;
    int sum = 0;
    rep(i, 0, n.length()){
    	char t = n[i];
    	if(t == '2'){
    		twos++;
		}
		if(t == '3'){
			threes++;
		}

		sum += (int)t - 48;
    }

	int k = sum % 9;

	if(k==0){
		cout << "YES" << endl;
		return;
	}

	rep(i, 0, twos+1){
		rep(j, 0, threes+1){
			if((k + 2*i + 6*j)%9 == 0){
				cout << "YES " << endl;
				return;
			}
		}
	}

	cout << "NO" << endl;

}

int32_t main(){
    fastio();
    int t = 1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

// Problem: C. Uninteresting Number
// Contest: Codeforces - Codeforces Round 991 (Div. 3)
// URL: https://codeforces.com/problemset/problem/2050/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define int long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define umap unordered_map
#define mii map<int, int>
#define pii pair<int, int>
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vi>
#define vpii vector<pii>
#define ff first
#define ss second
#define rep(i,x,y) for(int i=x; i<y; i++)
#define rrep(i,x,y) for(int i=x; i>=y; i--)
#define yn(f) f?cout<<"YES\n":cout<<"NO\n"
#define syn(f) f?cout<<"Yes\n":cout<<"No\n"
#define print(x) for(auto l:x) cout<<l<<" ";cout<<'\n';

int binexp(int a, int b,int M) {
    if(b==0) return 1;
    int res=binexp(a,b/2,M);
    if(b%2==1) return (((res*res)%M)*(a%M))%M;
    else return (res*res)%M;
}

int M = 1e9 + 7;
// int M = 998244353;

void solve(){
    int twos = 0;
    int threes = 0;
    int n;
    cin >> n;
    int sum = 0;
    while(n>0){
    	int t = n % 10;
    	if(t == 2){
    		twos++;
		}
		if(t == 3){
			threes++;
		}
		sum += t;
		n/=10;
    }

	int k = sum % 9;

	if(k==0){
		cout << "YES" << endl;
		return;
	}

	rep(i, 0, twos+1){
		rep(j, 0, threes+1){
			if((k + 2*i + 6*j)%9 == 0){
				cout << "YES " << endl;
				return;
			}
		}
	}

	cout << "NO" << endl;

}

int32_t main(){
    fastio();
    int t = 1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 1


#include <iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include <string>
#include<math.h>
#include<iomanip>
using namespace std;
void solve(){
    long long sum=0;
    long long two=0,three=0;
    string s;
    cin>>s;
    int len=s.length();
    for(int i=0;i<len;i++){
        sum+=s[i]-'0';
        if(s[i]=='2')two++;
        if(s[i]=='3')three++;
    }
    int mod=sum%9;
    if(mod==0){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==1&&((two>=4)||(three>=1&&two>=1))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==2&&(two>=8||(three>=1&&two>=5)||(three>=2&&two>=2))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==3&&(three>=1||two>=3)){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==4&&((two>=7)||(three>=1&&two>=4)||(three>=2&&two>=1))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==5&&two>=2){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==6&&((two>=6)||(three>=1&&two>=3)||(three>=2))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==7&&two>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==8&&((two>=5)||(three>=1&&two>=2))){
        cout<<"YES"<<"\n";
        return;
    }

    cout<<"NO\n";
}
int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

#include <iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include <string>
#include<math.h>
#include<iomanip>
using namespace std;
void solve(){
    long long sum=0;
    long long two=0,three=0;
    string s;
    cin>>s;
    int len=s.length();
    for(int i=0;i<len;i++){
        sum+=s[i]-'0';
        if(s[i]=='2')two++;
        if(s[i]=='3')three++;
    }
    int mod=sum%9;
    if(mod==0){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==2&&two>=1&&three>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==3&&three>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==4&&((two>=7)||(three==1&&two>=4)||(three==2&&two>=1))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==5&&two>=2){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==6&&((two>=6)||(three==1&&two>=3)||(three>=2))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==7&&two>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==8&&((two>=5)||(three==1&&two>=2))){
        cout<<"YES"<<"\n";
        return;
    }

    cout<<"NO\n";
}
int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

void solve()
{
    string s;
    cin >> s;
    int sum = 0;
    int two_sum = 0;
    int three_sum = 0;
    for (int i = 0; i < s.length(); i++)
    {
        sum += s[i] - '0';
        if (s[i] - '0' == 2)
        {
            two_sum += 2;
        }
        if (s[i] - '0' == 3)
        {
            three_sum += 6;
        }
    }
    if (sum % 9 == 0)
    {
        cout << "YES" << endl;
        return;
    }
    int fire = 9 - (sum % 9);
    while (fire <= two_sum + three_sum)
    {
        if (fire % 2 != 0)
        {
            fire += 9;
            continue;
        }
        int temp_fire = fire;
        int temp_two = two_sum;
        int temp_three = three_sum;
        while (temp_three >= 6 && temp_fire >= 6)
        {
            temp_fire -= 6;
            temp_three -= 6;
        }
        while (temp_two >= 2 && temp_fire >= 2)
        {
            temp_fire -= 2;
            temp_two -= 2;
        }
        if (temp_fire == 0)
        {
            cout << "YES" << endl;
            return;
        }
        fire += 18;
    }
    cout << "NO" << endl;
    return;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int INP;
    cin >> INP;
    while (INP--)
    {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

void solve()
{
    string s;
    cin >> s;
    int sum;
    int two_sum = 0;
    int three_sum = 0;
    for (int i = 0; i < s.length(); i++)
    {
        sum += s[i] - '0';
        if (s[i] - '0' == 2)
        {
            two_sum += 2;
        }
        if (s[i] - '0' == 3)
        {
            three_sum += 6;
        }
    }
    if (sum % 9 == 0)
    {
        cout << "YES" << endl;
        return;
    }
    int fire = 9 - (sum % 9);
    while (fire < two_sum + three_sum)
    {
        if (fire % 2 != 0)
        {
            fire += 9;
            continue;
        }
        int temp_fire = fire;
        int temp_two = two_sum;
        int temp_three = three_sum;
        while (temp_three && temp_fire)
        {
            temp_fire -= 6;
            temp_three -= 6;
        }
        while (temp_two && temp_fire)
        {
            temp_fire -= 2;
            temp_two -= 2;
        }
        if (temp_fire == 0)
        {
            cout << "YES" << endl;
            return;
        }
        fire += 18;
    }
    cout << "NO" << endl;
    return;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int INP;
    cin >> INP;
    while (INP--)
    {
        solve();
    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define f(i,a,b)        for(ll i=a;i<b;i++)
#define fm(i,a,b)       for(ll i=a;i>=b;i--)
#define fc(i,a,b,c)      for(ll i=a;i<b;i=i+c)
#define fmc(i,a,b,c)     for(ll i=a;i>=b;i=i-c)
#define fi(n)           f(i,0,n)
#define fj(n)           f(j,0,n)
#define fastfast()                      ios_base::sync_with_stdio(false); cin.tie(NULL);                    cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> vvll;
void yes(){cout << "YES" << endl;}
void no(){cout << "NO" << endl;}
void print(ll t) {cout << t << endl;}
void print(int t) {cout << t << endl;}
void print(string t) {cout << t << endl;}
void print(char t) {cout << t << endl;}
void print(lld t) {cout << t << endl;}
void print(double t) {cout << t << endl;}
void print(ull t) {cout << t << endl;}
void prn(ll n)
{
    fi(n)
    {
        cout<<i+1<<" ";
    }
    cout<<endl;
}
ll insqrt(ll n)
{
    ll s=0;
    ll e=1e10+1;
    ll ans=0;
    if(n<=0) return 0;
    while(s<=e)
    {
        ll mid=(s+e)/2;
        if(mid*mid<=n) {s=mid+1; ans=mid;}
        else e=mid-1;
    }
    return ans;
}

ll orar[100001][20];
void buildorar(ll arr[],ll n)
{
    fi(n) orar[i][0]=arr[i];
    fj(20)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        {
            orar[i][j+1]=orar[i][j]|orar[i+(1<<(j))][j];
        }
    }
}

ll calculate(ll l,ll r)
{
    ll ans=0;
    fm(i,20,0)
    {
        if((1<<i) <= r-l+1)
        {
            ans=ans|orar[l][i];
            l=l+(1<<i);
        }
    }
    return ans;
}

ll gcd(ll a,ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

ll fastex(ll a,ll n)
{
    ll p=1;
    ll ld;
    while(n>0)
    {
        ld=n&1;
        if(ld==1) p=p*a;
        a=a*a;
        n=n>>1;
    }
    return p;
}

class DisjointSet
{
public:
    vll size,parent;
    DisjointSet(ll n)
    {
        size.resize(n+1,1);
        parent.resize(n+1,0);
        for(int i=0;i<n+1;i++) parent[i]=i;
    }
    ll findPar(ll u)
    {
        if(parent[u]==u) return u;
        else return parent[u]=findPar(parent[u]);
    }
    void unionbysize(ll u,ll v)
    {
        ll pu=findPar(u);
        ll pv=findPar(v);
        if(pu==pv) return;
        if(size[pu]<size[pv])
        {
            parent[pu]=pv;
            size[pv]+=size[pu];
        }
        else
        {
            parent[pv]=pu;
            size[pu]+=size[pv];
        }
    }
    ll findsize(ll u)
    {
        ll pu=findPar(u);
        return size[pu];
    }
};

// Jai Bajrangbali

int main()
{
    fastfast();

    ll t;
    cin >> t;

    while(t--){
        ll n;
        cin >> n;

        ll two = 0;
        ll three = 0;
        ll s=0;

        while (n > 0) {
            ll d = n%10;
            n /= 10;
            if(d==2) two++;
            if(d==3) three++;
            s+=d;
        }

        if(s%9==0){
            cout << "YES" << endl;
            continue;
        }

        ll want = 9-(s%9);
        if(want%2!=0) want+=9;

        want /=2;
        ll sum=0;
        int flag = 0;
        for (int i=0;i<=two;i++) {
            for (int j=0;j<=three;j++) {
                if ((sum+i+j*3) == want) {
                    flag=1;
                    break;
                }
            }
            if(flag==1)break;
        }
        if(flag==1){
            print("YES");
            continue;
        }
        print("NO");
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define f(i,a,b)        for(ll i=a;i<b;i++)
#define fm(i,a,b)       for(ll i=a;i>=b;i--)
#define fc(i,a,b,c)      for(ll i=a;i<b;i=i+c)
#define fmc(i,a,b,c)     for(ll i=a;i>=b;i=i-c)
#define fi(n)           f(i,0,n)
#define fj(n)           f(j,0,n)
#define fastfast()                      ios_base::sync_with_stdio(false); cin.tie(NULL);                    cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> vvll;
void yes(){cout << "YES" << endl;}
void no(){cout << "NO" << endl;}
void print(ll t) {cout << t << endl;}
void print(int t) {cout << t << endl;}
void print(string t) {cout << t << endl;}
void print(char t) {cout << t << endl;}
void print(lld t) {cout << t << endl;}
void print(double t) {cout << t << endl;}
void print(ull t) {cout << t << endl;}
void prn(ll n)
{
    fi(n)
    {
        cout<<i+1<<" ";
    }
    cout<<endl;
}
ll insqrt(ll n)
{
    ll s=0;
    ll e=1e10+1;
    ll ans=0;
    if(n<=0) return 0;
    while(s<=e)
    {
        ll mid=(s+e)/2;
        if(mid*mid<=n) {s=mid+1; ans=mid;}
        else e=mid-1;
    }
    return ans;
}

ll orar[100001][20];
void buildorar(ll arr[],ll n)
{
    fi(n) orar[i][0]=arr[i];
    fj(20)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        {
            orar[i][j+1]=orar[i][j]|orar[i+(1<<(j))][j];
        }
    }
}

ll calculate(ll l,ll r)
{
    ll ans=0;
    fm(i,20,0)
    {
        if((1<<i) <= r-l+1)
        {
            ans=ans|orar[l][i];
            l=l+(1<<i);
        }
    }
    return ans;
}

ll gcd(ll a,ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

ll fastex(ll a,ll n)
{
    ll p=1;
    ll ld;
    while(n>0)
    {
        ld=n&1;
        if(ld==1) p=p*a;
        a=a*a;
        n=n>>1;
    }
    return p;
}

class DisjointSet
{
public:
    vll size,parent;
    DisjointSet(ll n)
    {
        size.resize(n+1,1);
        parent.resize(n+1,0);
        for(int i=0;i<n+1;i++) parent[i]=i;
    }
    ll findPar(ll u)
    {
        if(parent[u]==u) return u;
        else return parent[u]=findPar(parent[u]);
    }
    void unionbysize(ll u,ll v)
    {
        ll pu=findPar(u);
        ll pv=findPar(v);
        if(pu==pv) return;
        if(size[pu]<size[pv])
        {
            parent[pu]=pv;
            size[pv]+=size[pu];
        }
        else
        {
            parent[pv]=pu;
            size[pu]+=size[pv];
        }
    }
    ll findsize(ll u)
    {
        ll pu=findPar(u);
        return size[pu];
    }
};

// Jai Bajrangbali

int main()
{
    fastfast();

    ll t;
    cin >> t;

    while(t--){
        string st;
        cin >> st;

        int two = 0;
        int three = 0;
        int s=0;

        for(auto it:st){
            if(it=='2')two++;
            if(it=='3')three++;
            s+=(it-'0');
            s%=9;
        }

        int flag = 0;
        for (int i=0;i<=two;i++) {
            for (int j=0;j<=three;j++) {
                ll ts = s;
                ts += 2*i+j*6;
                if (ts%9 == 0) {
                    flag=1;
                    break;
                }
            }
            if(flag==1)break;
        }
        if(flag==1){
            print("YES");
            continue;
        }
        else print("NO");
    }

    return 0;
}

Answer: 2


#include<bits/stdc++.h>

using namespace std;

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

using ll=long long;

#define int long long

signed main()

{

    IOS;

    int t=0;

    cin>>t;

    while(t--)

    {

        string s;

        cin>>s;

        int cnt2=0,cnt3=0,fg=0,ans=0;

        for(int i=0;i<s.size();i++)

        {

            ans+=(s[i]-'0');

            if(s[i]-'0'==2)

            cnt2++;

            if(s[i]-'0'==3)

            cnt3++;

        }

        for(int i=0;i<=cnt2;i++)

        {

            for(int j=0;j<=cnt3;j++)

            {

                if((ans+2*i+6*j)%9==0)

                {

                    fg=1;

                    break;

                }

            }

        }

        if(fg==1) cout<<"YES\n";

        else cout<<"NO\n";

    }

    return 0;

}

#include<bits/stdc++.h>

using namespace std;

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

using ll=long long;

#define int long long

int res[100010];

signed main()

{

    IOS;

    int t=0;

    cin>>t;

    while(t--)

    {

        string s;

        cin>>s;

        int ans=0,fg=0;

        for(int i=0;i<s.size();i++)

        {

            res[i]=s[i]-'0';

            ans+=res[i];

        }

        sort(res,res+s.size());

        for(int i=0;i<s.size();i++)

        {

            if(res[i]<=3)

            {

                ans+=res[i]*res[i]-res[i];

            }

            if(ans%9==0)

            {

                fg=1;

            }

        }

        if(fg==1)

        {

            cout<<"YES\n";

        }

        else cout<<"NO\n";

    }

    return 0;

}

Answer: 1


#include <bits/stdc++.h>
const char nl = '\n';
using ll = long long;
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        ll sum = 0;
        ll cnt2 = 0; ll cnt3 = 0;
        for (int i=0; i<s.size(); i++)
        {
            int digit = s[i]-'0';
            sum += digit;
            if (digit == 2)
                cnt2 += 1;
            else if (digit == 3)
                cnt3 += 1;
        }
        cnt2 = min(cnt2,(ll)8);
        cnt3 = min(cnt3,(ll)8);
        int validate = 0;
        for(int i=0; i<=cnt2; i++)
        {
            if (i>0)
                sum += 2;
            for (int j=0; j<=cnt3; j++)
            {
                if (j>0)
                    sum += 6;
                if (sum%9 == 0)
                {
                    cout << "YES" << nl;
                    validate = 1;
                    break;
                }
            }
            sum -= 6*cnt3;
        }
        if (validate == 0)
            cout << "NO" << nl;
    }
    return 0;
}

#include <bits/stdc++.h>
const char nl = '\n';
using ll = long long;
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        ll sum = 0;
        ll cnt2 = 0; ll cnt3 = 0;
        for (int i=0; i<s.size(); i++)
        {
            int digit = s[i]-'0';
            sum += digit;
            if (digit == 2)
                cnt2 += 1;
            else if (digit == 3)
                cnt3 += 1;
        }
        cnt2 = min(cnt2,(ll)8);
        cnt3 = min(cnt3,(ll)8);
        int validate = 0;
        for(int i=0; i<=cnt2 ; i++)
        {
            if (i>0)
                sum += 2;
            for (int j=0; j<=cnt3; j++)
            {
                if (j>0)
                    sum += 6;
                if (sum%9 == 0)
                {
                    cout << "YES" << nl;
                    validate = 1;
                    break;
                }
            }
            sum -= 6*cnt3;
            if (validate == 1)
                break;
        }
        if (validate == 0)
            cout << "NO" << nl;
    }
    return 0;
}

Answer: 2