Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 296 ms):
import java.util.*;
import java.io.*;

public class Main {
    static Scanner f;
    static PrintWriter out;

    public static void main(String[] args) throws Exception {
        // f = new Scanner(new File("input.txt"));
        // out = new PrintWriter(new File("output.txt"));
        f = new Scanner(System.in);
        out = new PrintWriter(System.out);

        int t = f.nextInt();
        f.nextLine();
        while(t-- > 0) {
            solve();
        }

        out.close();
    }


    public static void solve() {
        String s = f.nextLine();
        int n = s.length();
        int sum = 0;
        int count2 = 0;
        int count3 = 0;
        for(int i = 0; i < n; i++) {
            sum += s.charAt(i)-'0';
            if(s.charAt(i) == '2') {
                count2++;
            } else if(s.charAt(i) == '3') {
                count3++;
            }
        }

        for(int i = 0; i <= Math.min(50, count2); i++) {
            for(int j = 0; j <= Math.min(50, count3); j++) {
                if((sum+2*i+6*j)%9 == 0) {
                    out.println("YES");
                    return;
                }
            }
        }
        out.println("NO");
    }


}

Solution 2 (runtime: 296 ms):


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.*;

 

public class Main {

    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 {

                if (st.hasMoreTokens()) {

                    str = st.nextToken("\n");

                }

                else {

                    str = br.readLine();

                }

            }

            catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }

    public static int check(ArrayList<Integer> arr,int i,int sum,int k,int dp[][]){

            

            if(i==arr.size()){

                return (sum+k)%9==0?1:0;

            }

            if(dp[i][sum]==-1){

            int take= check(arr,i+1,(sum+arr.get(i))%9,k,dp);

            int ntake=check(arr,i+1,sum,k,dp);

             dp[i][sum]= (take+ntake)>0?1:0;

            }

            return dp[i][sum];

    }

    public static boolean helper(ArrayList<Integer> arr,int k){

        int n=arr.size();

        int dp[][]=new int[n][10];

       

            for(int a[]:dp){

                Arrays.fill(a,-1);

            }

        return check(arr,0,0,k,dp)>0;

    }

    public static void solve(FastReader sc){

        String s=sc.next(); 

        int sum=0;

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

        for(char c:s.toCharArray()){

            int num=c-'0';

            sum=(sum+num)%9;

            if(num==2){

                arr.add(2);

            }

            else if(num==3){

                arr.add(6);

            }

        }

        if(sum!=0){

           if(helper(arr,sum)){

            System.out.println("YES");

           }

           else{

            System.out.println("NO");

           }

        }

        else{

            System.out.println("YES");

        }





    }

    



    

    

    public static void main(String args[]) {

        FastReader sc = new FastReader();

        int t=sc.nextInt();

        while(t-- >0){

            solve(sc);

        }

    }

}