Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 328 ms):
import java.util.*;
public class Main
{
	public static void main(String[] args) {
	    Scanner sc=new Scanner(System.in);
	    int t=sc.nextInt();
	    while(t-->0){
	        String n=sc.next();
	        int s=0;
	        HashMap<Integer,Integer>hm=new HashMap();
	        for(int i=0;i<n.length();i++){
	            s+=n.charAt(i)-'0';
	            if((n.charAt(i)-'0')==3)
	            hm.put(6,hm.getOrDefault(6,0)+1);
	            else if((n.charAt(i)-'0')==2)
	            hm.put(2,hm.getOrDefault(2,0)+1);

	            
	        }

            int f=0;
	        if(s%9==0)
	        		System.out.println("YES");
	        		else{
	        		    for(int i=0;i<=hm.getOrDefault(2,0);i++){
	        		        if(f==1)
	        		        break;
	        		        
	        		        for(int j=0;j<=hm.getOrDefault(6,0);j++){
	        		            if((s+i*2+j*6)%9==0)
	        		            {
	        		                f=1;
	        		                break;
	        		            }
	        		        }
	        		    }
	        		    System.out.println((f==1)?"YES":"NO");
	                    }



	        
	    }
	}
}


Solution 2 (runtime: 249 ms):

import java.io.*;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author raito
 * @since 2024/12/26
 */
public class Cr_991 {
    public static void main(String[] args) {
        Fast fast = new Fast();
        new Cr_991().C(fast);
        fast.flush();
    }

    public void C(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            String s = fast.nextLine();
            long sum = 0;
            int a2 = 0;
            int a3 = 0;
            for (int i = 0; i < s.length(); i++) {
                int p = s.charAt(i) - '0';
                sum += p;
                if (p == 2) {
                    a2++;
                }
                if (p == 3) {
                    a3++;
                }
            }
            int d = (int) (sum % 9);
            if (d == 0) {
                fast.write("YES\n");
                continue;
            }
            // 6, 3
            // 2, 4, 6, 8, 1, 3, 5, 7, 0, 2, 4, 6, 8, 1, 3, 5, 7, 0
            int p = 9 - d;
            if (p == 2) {
                if (a2 > 0) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 4) {
                if (a2 > 1) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 3) {
                if (a3 >= 2 || (a3 == 1 && a2 >= 3 ) || a2 >= 6) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 1) {
                if (a2 >= 5 || (a3 >= 1 && a2 >= 2)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 5) {
                if (a2 >= 7 || (a3 >= 2 && a2 >= 1) || (a3 == 1 && a2 >= 4)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 6) {
                if (a3 >= 1 || a2 >= 3) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 7) {
                if (a2 >= 8 || (a3 == 1 && a2 >= 5) || (a3 > 1 && a2 >= 2)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else {
                if (a2 >= 4 || (a3 >= 1 && a2 >= 1)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            }

        }
    }

    public void B(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            int n = fast.nextInt();
            long[] a = new long[n];
            long asum = 0;
            long bsum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = fast.nextLong();
                if (((i + 1) & 1) == 1) {
                    asum += a[i];
                } else {
                    bsum += a[i];
                }
            }
            long sum = asum + bsum;
            if (sum % n != 0) {
                fast.write("NO\n");
                continue;
            }
            long target = sum / n;
            int blen = n / 2;
            int alen = n - blen;
            if (asum != alen * target || bsum != target * blen) {
                fast.write("NO\n");
            } else {
                fast.write("YES\n");
            }
        }
    }

    public void A(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            int n = fast.nextInt();
            int m = fast.nextInt();
            int ans = 0;
            for(int i = 0; i < n; i++) {
                String s = fast.nextLine();
                if (s.length() <= m) {
                    m -= s.length();
                    ans++;
                } else {
                    m = -1;
                }
            }
            fast.write(ans + "\n");
        }
    }

    public static class Fast {
        BufferedReader br;
        StringTokenizer st;
        BufferedWriter bw;

        public Fast() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            return str;
        }

        void write(String s) {
            if (bw == null) {
                bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }
            try {
                bw.write(s);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        void write(int c) {
            if (bw == null) {
                bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }
            try {
                bw.write(c);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        void flush() {
            if (bw != null) {
                try {
                    bw.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}