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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class main {
    static PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
    static FastReader in = new FastReader();
    public static int n;  
    public static void main(String[] args) {
        int t = in.nextInt();
        while (t-- > 0) {
            String p = in.next();
            n = p.length();
            long sum = 0;
            for (int i = 0; i < n; i++) {
                int l = p.charAt(i) - '0';
                sum += l;
            }
            if (sum % 9 == 0) {
                pw.println("YES");
            } else {
                sum %= 9;
                sum = 9 - sum;

                int count2 = 0;
                int count3 = 0;
                for (int i = 0; i < n; i++) {
                    int l = p.charAt(i) - '0';
                    if (l == 2) {
                        count2++;
                    } else if (l == 3) {
                        count3++;
                    }
                }
                if (sum % 2 != 0) {
                    sum += 9;
                }
                while(count3 > 0 && sum >= 6) {
                    count3--;
                    sum -= 6;
                }
                while(count2 > 0 && sum >= 2) {
                    count2--;
                    sum -= 2;
                }
                if (sum == 0) {
                    pw.println("YES");
                } else {
                    pw.println("NO");
                }
            }
        }
        pw.close();
    }
    
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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) {
                e.printStackTrace();
            }
            return str;
        }
    }
}