Problem: 2050C - Uninteresting Number
Language: java

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

public class UninterestingNumber {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tokenizer = new StringTokenizer(in.readLine());

        int t = Integer.parseInt(tokenizer.nextToken());
        ArrayList<String> ans = new ArrayList<>();

        for (int i = 0; i < t; i++) {
            tokenizer = new StringTokenizer(in.readLine());
            String n = tokenizer.nextToken();
            int num = simplify(n);

            if (num == 0) {
                ans.add("YES");
                continue;
            }

            int num2 = numTwos(n);
            int num3 = numThrees(n);

            boolean canAdd = false;

            if (num == 1) {
                if (num3 >= 1 && num2 >= 1) {
                    canAdd = true;
                } else if (num2 >= 4) {
                    canAdd = true;
                }
            } else if (num == 2) {
                if (num3 >= 2 && num2 >= 2) {
                    canAdd = true;
                } else if (num3 == 1 && num2 >= 5) {
                    canAdd = true;
                } else if (num2 >= 8) {
                    canAdd = true;
                }
            } else if (num == 3) {
                if (num3 >= 1 || num2 >= 3) {
                    canAdd = true;
                }
            } else if (num == 4) {
                if (num3 >= 2 && num2 >= 1) {
                    canAdd = true;
                } else if (num3 == 1 && num2 >= 4) {
                    canAdd = true;
                } else if (num2 >= 7) {
                    canAdd = true;
                }
            } else if (num == 5) {
                if (num2 >= 2) {
                    canAdd = true;
                }
            } else if (num == 6) {
                if (num3 >= 2) {
                    canAdd = true;
                } else if (num3 == 1 && num2 >= 3) {
                    canAdd = true;
                } else if (num2 >= 6) {
                    canAdd = true;
                }
            } else if (num == 7) {
                if (num2 >= 1) {
                    canAdd = true;
                }
            } else if (num == 8) {
                if (num3 >= 1 && num2 >= 2) {
                    canAdd = true;
                } else if (num2 >= 5) {
                    canAdd = true;
                }
            }

            if (canAdd) {
                ans.add("YES");
                continue;
            }
            ans.add("NO");
        }

        for (String s : ans) {
            System.out.println(s);
        }
    }

    public static int numTwos(String x) {
        int ret = 0;
        for (char a : x.toCharArray()) {
            if ((a - '0') == 2) ret++;
        }
        return ret;
    }

    public static int numThrees(String x) {
        int ret = 0;
        for (char a : x.toCharArray()) {
            if ((a - '0') == 3) ret++;
        }
        return ret;
    }

    public static int simplify(String x) {
        int ret = 0;
        for (char a : x.toCharArray()) {
            ret += (a - '0');
        }
        return ret % 9;
    }
}

Solution 2 (runtime: 249 ms):
//package com.aakash.Contest.C991;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class C {
    static BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {

        StringTokenizer s1
                = new StringTokenizer(br.readLine());
        int t = Integer.parseInt(s1.nextToken());
        while (t-- > 0) {
            StringBuilder sb=new StringBuilder(br.readLine());
            int n = sb.length();
            boolean is=solve(sb,n);
            if(is) System.out.println("YES");
            else System.out.println("NO");
        }

    }

    static boolean solve(StringBuilder sb,int n) {
        int two=0,three=0;
        int sum=0;
        for(int i=0;i<n;i++) {
            int curr=sb.charAt(i)-'0';
            if(curr==2) two++;
            if(curr==3) three++;
            sum+=curr;
            sum%=9;
        }
        if((two>=5&&three>=5) ||sum==0) return true;
        sum=9-sum;
        for(int x=0;x<=two;x++) {
            for(int y=0;y<=three;y++) {
                long curr=x*2;
                curr+=y*6;
                if(curr%9==sum) return true;
            }
        }
        return false;
    }
}