Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 312 ms):
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
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();
            long sum=0;
            long cnt1=0;
            long cnt2=0;
            for(long i=0; i<n.length(); i++){
                char ch=n.charAt((int)i);
                if(ch=='2'){
                    cnt1++;
                }
                else if(ch=='3'){
                    cnt2++;
                }
                sum+=(ch-'0');
                sum=sum%9;
            }
                boolean check=false;
                for(int i=0; i<=cnt1; i++){
                    for(int j=0; j<=cnt2; j++){
                        long temp1=sum;
                        temp1=temp1+(i*2)+(j*6);
                        if(temp1%9==0){
                            check=true;
                            break;
                        }
                    }
                    if(check==true){
                        break;
                    }
                }
                if(check==true){
                    System.out.println("YES");
                }
                else{
                    System.out.println("NO");
                }
            t--;
        }
    }
}

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

        }

    }

}