Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 249 ms):

// /* Jai Guru Dev*/
// /*सब सुख लहै तुम्हारी सरना ।
// तुम रक्षक काहू को डरना ॥*/
// /*Don't cry in a corner, if you want something, mehnat kar, best ban, aur cheen le.*/
// //Modulus properties
// //1 (a+b)%y = (a%y+b%y)%y also 
// // <if to find pairs st (a+b)%y==0 one is (a%y) and other is b%y==((y-(a%y))%y)> 
// // < if (a+b)%y == (c+b)%y  then a%y = c%y (in  terms of remainder) >
// // 2 (a-b)%y = (a%y-b%y+y)%y also  
// //   < if (a-b)%y ==0 then a%y = b%y  >
// import java.io.BufferedReader;
// import java.io.InputStream;
// import java.io.InputStreamReader;
// import java.io.IOException;
// import java.util.StringTokenizer;
// import java.util.LinkedList;
// import java.util.Arrays;
// import java.util.*;

// /*
// learnings
// so the max value of the arrange ment (a[0]-a[1])+(a[1]-a[2])......(a[n-2]-a[n-1]) = Max(a)-Min(a)
// */
// public class one {

//     static final int INF = Integer.MAX_VALUE;
//     static final long MOD = 1000000007;

//     static class FastScanner {
//         BufferedReader br;
//         StringTokenizer st;

//         FastScanner(InputStream is) {
//             br = new BufferedReader(new InputStreamReader(is));
//         }

//         String next() {
//             while (st == null || !st.hasMoreTokens()) {
//                 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;
//         }
//     }

//     static class Graph {
//         int V;
//         LinkedList<Integer>[] adjList;

//         @SuppressWarnings("unchecked")
//         Graph(int V) {
//             this.V = V;
//             adjList = new LinkedList[V];
//             for (int i = 0; i < V; i++)
//                 adjList[i] = new LinkedList<>();
//         }

//         void addEdge(int u, int v) {
//             adjList[u].add(v);
//         }

//         // Add more graph algorithms as needed
//     }

//     static class DSU {
//         int[] parent;
//         int[] rank;

//         DSU(int n) {
//             parent = new int[n];
//             rank = new int[n];
//             for (int i = 0; i < n; i++) {
//                 parent[i] = i;
//                 rank[i] = 0;
//             }
//         }

//         int find(int x) {
//             if (parent[x] != x)
//                 parent[x] = find(parent[x]);
//             return parent[x];
//         }

//         void union(int x, int y) {
//             int xRoot = find(x);
//             int yRoot = find(y);
//             if (xRoot == yRoot)
//                 return;
//             if (rank[xRoot] < rank[yRoot])
//                 parent[xRoot] = yRoot;
//             else if (rank[xRoot] > rank[yRoot])
//                 parent[yRoot] = xRoot;
//             else {
//                 parent[yRoot] = xRoot;
//                 rank[xRoot]++;
//             }
//         }
//     }

//     static int gcd(int a, int b) {
//         while (b != 0) {
//             int temp = b;
//             b = a % b;
//             a = temp;
//         }
//         return a;
//     }

//     static long gcd(long a, long b) {
//         while (b != 0) {
//             long temp = b;
//             b = a % b;
//             a = temp;
//         }
//         return a;
//     }

//     static long power(long x, long y, long p) {
//         long res = 1;
//         x = x % p;

//         while (y > 0) {
//             if (y % 2 == 1)
//                 res = (res * x) % p;
//             y = y >> 1;
//             x = (x * x) % p;
//         }
//         return res;
//     }

//     static boolean[] sieveOfEratosthenes(int n) {
//         boolean[] prime = new boolean[n + 1];
//         Arrays.fill(prime, true);
//         prime[0] = prime[1] = false;
//         for (int p = 2; p * p <= n; p++) {
//             if (prime[p]) {
//                 for (int i = p * p; i <= n; i += p)
//                     prime[i] = false;
//             }
//         }
//         return prime;
//     }

//     // jai guru dev
//     static class Pair {
//         int x;
//         int y;

//         Pair(int x, int y) {
//             this.x = x;
//             this.y = y;
//         }
//     }

//     public static void main(String[] args) {
//         FastScanner sc = new FastScanner(System.in);
//         int t = sc.nextInt();
//         while (t-- > 0) {
//             int n = sc.nextInt();
//             int a[] = new int[n];
//             for (int i = 0; i < n; i++) {
//                 a[i] = sc.nextInt();
//             }
//             Arrays.sort(a);
//             int ans = 0;
//             long pref[] = new long[n];
//             pref[0] = a[0];
//             for (int i = 1; i < n; i++) {
//                 pref[i] = pref[i - 1] + a[i];
//             }
//             int i = 0;
//             int j = n - 1;
//             long x = 0;
//             long c = 0;
//             long ss = 0;
//             long ops = 0;
//             while (i <= j) {
//                 int si = i;

//                 while (i < j && ss + a[i] <= a[j]) {
//                     ss += a[i];
//                     i++;
//                 }

//                 if (ss == a[j]) {
//                     ops += a[j];
//                     ops++;
//                     ss = 0;
//                     j--;
//                     continue;
//                 }
//                 if (i == j) {
//                     if(ss==0){
//                         ops+=a[j];
//                         j--;
//                         break;
//                     }
//                     int s = 0;
//                     int e = a[i];
//                     // System.out.println(ss + " " + a[i]);
//                     long aa = -1;
//                     while (s <= e) {
//                         int mid = (s + e) / 2;
//                         if (ss + mid >= a[j] - mid) {
//                             aa = mid;
//                             e = mid - 1;
//                         } else {
//                             s = mid + 1;
//                         }
//                     }
//                     ops += (ss + aa);
//                     ops++;
//                     a[i] -= aa;
//                     i++;
//                     ss = 0;
//                     j--;
//                     break;
//                 }
//                 int s = 0;
//                 int e = a[i];
//                 long aa = -1;
//                 while (s <= e) {
//                     int mid = (s + e) / 2;
//                     if (ss + mid >= a[j]) {
//                         aa = mid;
//                         e = mid - 1;
//                     } else {
//                         s = mid + 1;
//                     }
//                 }
//                 ops += (ss + aa);
//                 // System.out.println(aa);
//                 ops++;
//                 a[i] -= aa;

//                 ss = 0;
//                 j--;
//             }
//             System.out.println(ops);
//         }
//     }
// }
// // 5
// // 6 6 6 6 4

/* Jai Guru Dev*/
/*सब सुख लहै तुम्हारी सरना ।
तुम रक्षक काहू को डरना ॥*/
/*Don't cry in a corner, if you want something, mehnat kar, best ban, aur cheen le.*/
//Modulus properties
//1 (a+b)%y = (a%y+b%y)%y also 
// <if to find pairs st (a+b)%y==0 one is (a%y) and other is b%y==((y-(a%y))%y)> 
// < if (a+b)%y == (c+b)%y  then a%y = c%y (in  terms of remainder) >
// 2 (a-b)%y = (a%y-b%y+y)%y also  
//   < if (a-b)%y ==0 then a%y = b%y  >
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.LinkedList;
import java.util.Arrays;
import java.util.*;

/*
learnings
so the max value of the arrange ment (a[0]-a[1])+(a[1]-a[2])......(a[n-2]-a[n-1]) = Max(a)-Min(a)
*/
public class one {

    static final int INF = Integer.MAX_VALUE;
    static final long MOD = 1000000007;

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                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;
        }
    }

    static class Graph {
        int V;
        LinkedList<Integer>[] adjList;

        @SuppressWarnings("unchecked")
        Graph(int V) {
            this.V = V;
            adjList = new LinkedList[V];
            for (int i = 0; i < V; i++)
                adjList[i] = new LinkedList<>();
        }

        void addEdge(int u, int v) {
            adjList[u].add(v);
        }

        // Add more graph algorithms as needed
    }

    static class DSU {
        int[] parent;
        int[] rank;

        DSU(int n) {
            parent = new int[n];
            rank = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                rank[i] = 0;
            }
        }

        int find(int x) {
            if (parent[x] != x)
                parent[x] = find(parent[x]);
            return parent[x];
        }

        void union(int x, int y) {
            int xRoot = find(x);
            int yRoot = find(y);
            if (xRoot == yRoot)
                return;
            if (rank[xRoot] < rank[yRoot])
                parent[xRoot] = yRoot;
            else if (rank[xRoot] > rank[yRoot])
                parent[yRoot] = xRoot;
            else {
                parent[yRoot] = xRoot;
                rank[xRoot]++;
            }
        }
    }

    static int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    static long gcd(long a, long b) {
        while (b != 0) {
            long temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    static long power(long x, long y, long p) {
        long res = 1;
        x = x % p;

        while (y > 0) {
            if (y % 2 == 1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }

    static boolean[] sieveOfEratosthenes(int n) {
        boolean[] prime = new boolean[n + 1];
        Arrays.fill(prime, true);
        prime[0] = prime[1] = false;
        for (int p = 2; p * p <= n; p++) {
            if (prime[p]) {
                for (int i = p * p; i <= n; i += p)
                    prime[i] = false;
            }
        }
        return prime;
    }

    static long sumofdigits(String s) {
        long sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum += s.charAt(i) - '0';
        }
        return sum;
    }

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            String s = sc.next();
            long sum = sumofdigits(s);
            int f2 = 0;
            int f3 = 0;
            for (int i = 0; i < s.length(); i++) {
                if ((s.charAt(i) - '0') == 2) {
                    f2++;
                }
                if ((s.charAt(i) - '0') == 3) {
                    f3++;
                }
            }
            if (f2 == 0 && f3 == 0) {
                if (sum % 9 == 0) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            } else {
                long val = (sum) % 9;
                // System.out.println(val);
                if (val == 0) {
                    System.out.println("YES");
                } else if (val == 1) {
                    if (f2 >= 4) {
                        System.out.println("YES");
                    } else if (f3 >= 1 && f2 >= 1) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");
                    }
                } else if (val == 2) {
                    if (f2 >= 8) {
                        System.out.println("YES");
                    }
                    else if (f3 >= 1 && f2 >= 5) {
                        System.out.println("YES");
                    }
                    else if(f3>=2&&f2>=2){
                        System.out.println("YES");
                    }
                    else {
                        System.out.println("NO");
                    }
                } else if (val == 3) {
                    if (f3 >= 1) {
                        System.out.println("YES");
                    } else if (f2 >= 3) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");
                    }
                } else if (val == 4) {
                    if (f2 >= 7) {
                        System.out.println("YES");
                    } else if (f3 >= 2 && f2 >= 1) {
                        System.out.println("YES");
                    } else if (f2 >= 4 && f3 >= 1) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");
                    }
                } else if (val == 5) {
                    if (f2 >= 2) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");
                    }
                } else if (val == 6) {
                    if (f2 >= 6) {
                        System.out.println("YES");
                    } else if (f3 >= 2) {
                        System.out.println("YES");
                    } else if (f2 >= 3 && f3 >= 1) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");
                    }
                } else if (val == 7) {
                    if (f2 >= 1) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");
                    }
                } else if (val == 8) {
                    if (f2 >= 2 && f3 >= 1) {
                        System.out.println("YES");
                    } else if (f2 >= 5) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");

                    }
                }
            }
        }
    }
}


Solution 2 (runtime: 249 ms):

import java.io.*;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author raito
 * @since 2024/12/26
 */
public class Cr_991 {
    public static void main(String[] args) {
        Fast fast = new Fast();
        new Cr_991().C(fast);
        fast.flush();
    }

    public void C(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            String s = fast.nextLine();
            long sum = 0;
            int a2 = 0;
            int a3 = 0;
            for (int i = 0; i < s.length(); i++) {
                int p = s.charAt(i) - '0';
                sum += p;
                if (p == 2) {
                    a2++;
                }
                if (p == 3) {
                    a3++;
                }
            }
            int d = (int) (sum % 9);
            if (d == 0) {
                fast.write("YES\n");
                continue;
            }
            // 6, 3
            // 2, 4, 6, 8, 1, 3, 5, 7, 0, 2, 4, 6, 8, 1, 3, 5, 7, 0
            int p = 9 - d;
            if (p == 2) {
                if (a2 > 0) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 4) {
                if (a2 > 1) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 3) {
                if (a3 >= 2 || (a3 == 1 && a2 >= 3 ) || a2 >= 6) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 1) {
                if (a2 >= 5 || (a3 >= 1 && a2 >= 2)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 5) {
                if (a2 >= 7 || (a3 >= 2 && a2 >= 1) || (a3 == 1 && a2 >= 4)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 6) {
                if (a3 >= 1 || a2 >= 3) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 7) {
                if (a2 >= 8 || (a3 == 1 && a2 >= 5) || (a3 > 1 && a2 >= 2)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else {
                if (a2 >= 4 || (a3 >= 1 && a2 >= 1)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            }

        }
    }

    public void B(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            int n = fast.nextInt();
            long[] a = new long[n];
            long asum = 0;
            long bsum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = fast.nextLong();
                if (((i + 1) & 1) == 1) {
                    asum += a[i];
                } else {
                    bsum += a[i];
                }
            }
            long sum = asum + bsum;
            if (sum % n != 0) {
                fast.write("NO\n");
                continue;
            }
            long target = sum / n;
            int blen = n / 2;
            int alen = n - blen;
            if (asum != alen * target || bsum != target * blen) {
                fast.write("NO\n");
            } else {
                fast.write("YES\n");
            }
        }
    }

    public void A(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            int n = fast.nextInt();
            int m = fast.nextInt();
            int ans = 0;
            for(int i = 0; i < n; i++) {
                String s = fast.nextLine();
                if (s.length() <= m) {
                    m -= s.length();
                    ans++;
                } else {
                    m = -1;
                }
            }
            fast.write(ans + "\n");
        }
    }

    public static class Fast {
        BufferedReader br;
        StringTokenizer st;
        BufferedWriter bw;

        public Fast() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            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) {
                System.out.println(e.getMessage());
            }
            return str;
        }

        void write(String s) {
            if (bw == null) {
                bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }
            try {
                bw.write(s);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        void write(int c) {
            if (bw == null) {
                bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }
            try {
                bw.write(c);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        void flush() {
            if (bw != null) {
                try {
                    bw.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}