Problem: 2050C - Uninteresting Number
Language: java

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

public class C_Uninteresting_Number{
    static HashMap<Integer, Boolean> hm;
    public static boolean func(int num, int two, int three){
        if(num%9==0)return true;
        if(two==0&&three==0)return false;
        if(hm.containsKey(num))return hm.get(num);
        boolean t=false, th=false;
        if(two>0){
            t = func(num+2, two-1, three);
        }
        if(three>0){
            th = func(num+6, two, three-1);
        }
        hm.put(num, t|th);
        return t|th;
    }
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int t = scn.nextInt();
        while(t-->0){
            String s = scn.next();
            int n = s.length();
            int num = 0;
            int two = 0, three=0;
            for(int i = 0; i < n; i++){
                num += (s.charAt(i)-'0');
                if(s.charAt(i)=='2')two++;
                if(s.charAt(i)=='3')three++;
            }
            hm = new HashMap<>();
            boolean ans = func(num, two, three);
            if(ans)
            System.out.println("YES");
            else System.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);

        }

    }

}