Problem: 2050C - Uninteresting Number
Language: java

Code 1:
import java.io.*;
import java.util.*;

public class TLE {
  public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  static FastReader in = new FastReader();

  public static void main(String[] args) throws Exception {
    int testCases = in.nextInt();
    while (testCases-- > 0) {
      solve();
    }
    out.close();
  }

  private static void solve() {
    long sum = 0;
    String s = in.next();
    int two = 0, three = 0;
    for (char c : s.toCharArray()) {
      sum += c - '0';
      if (c == '2') {
        two++;
      } else if (c == '3') {
        three++;
      }
    }
    for (int t = 0; t <= two; t++) {
      for (int th = 0; th <= three; th++) {
        if ((sum + 2 * t + th * 6) % 9 == 0) {
          System.out.println("YES");
          return;
        }
      }
    }
    System.out.println("NO");

  }

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


Code 2:
import java.io.*;
import java.util.*;

public class TLE {
  public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  static FastReader in = new FastReader();

  public static void main(String[] args) throws Exception {
    int testCases = in.nextInt();
    while (testCases-- > 0) {
      solve();
    }
    out.close();
  }

  private static void solve() {
    long sum = 0;
    String s = in.next();
    int two = 0, three = 0;
    for (char c : s.toCharArray()) {
      sum += c - '0';
      if (c == '2') {
        two++;
      } else if (c == '3') {
        three++;
      }
    }
    if (sum % 9 == 0) {
      System.out.println("YES");
    } else {
      long modN = sum % 9;
      long mod = 9 - modN;
      if (mod == 2 && two >= 1) {
        System.out.println("YES");
      } else if (mod == 4 && two >= 2) {
        System.out.println("YES");
      } else if (mod == 3 && three >= 2) {
        System.out.println("YES");
      } else if (mod == 6 && (two >= 3 || three >= 2)) {
        System.out.println("YES");
      } else if (mod == 8 && (three >= 2 && two >= 1) || (two >= 4)) {
        System.out.println("YES");
      } else if (mod == 5 && two >= 1 && three >= 1) {
        System.out.println("YES");
      } else if (mod == 7 && two >= 2 && three >= 1) {
        System.out.println("YES");
      } else {
        System.out.println("NO");
      }
    }

  }

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


Answer: 1