Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 61 ms):

// ॐ
//---------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>
#define fast_io                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define endll '\n'
#define print(x) cout << x << endll
#define py cout << "YES\n"
#define pn cout << "NO\n"
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define fer(i, a, b) for (ll i = a; i <= b; i++)
#define frr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define int int64_t
#define all(v) v.begin(), v.end()
#define all_rev(v) v.rbegin(), v.rend()
#define vecp(v)           \
    for (auto i : v)      \
        cout << i << ' '; \
    cout << endll
#define vecpp(v)     \
    for (auto i : v) \
    cout << i.ff << ' ' << i.ss << endl
#define take(x) \
    int x;      \
    cin >> x
#define popcnt(n) __builtin_popcountll(n)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define debug(x) cout << #x << ' ' << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvll = vector<vll>;
using stll = set<ll>;
using mpll = map<ll, ll>;
using mpvll = map<ll, vll>;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 2e5 + 10;

// priority_queue <int, vector<int>, greater<int> > p;
//  __builtin_clz(x): the number of zeros at the beginning of the number
//  __builtin_ctz(x): the number of zeros at the end of the number
//  __builtin_popcountll(x): the number of ones in the number
//  __builtin_parity(x): the parity (even or odd) of the number of ones
//  __builtin_ffs(int) finds the index of the first (most right) set bit
void init_code()
{
    fast_io;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
    freopen("error.txt", "w", stderr);
#endif
}
bool isPrime(const ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 or n % 3 == 0)
        return false;
    for (ll i = 5; i * i <= n; i += 6)
        if (n % i == 0 or n % (i + 2) == 0)
            return false;
    return true;
}
ull power(ll x, ll y)
{
    ull res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % MOD;
        x = (x * x) % MOD;
        y >>= 1;
    }
    return res;
}
ull modInvPow(const ll n)
{
    return power(n, MOD - 2);
}
bool sortByCond(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
    /*
        if (a.ff == b.ff)
            return a.ss < b.ss;
        else
            return a.ff < b.ff;
    */
    return a.ss < b.ss;
}

void ramram()
{
    string s;
    cin >> s;
    int sum = 0;
    int cnt2 = 0, cnt3 = 0;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == '2')
            cnt2++;
        if (s[i] == '3')
            cnt3++;
        sum += s[i] - '0';
    }
    int rem = sum % 9;
    rem = 9 - rem;
    // cout << cnt2 << " " << cnt3 << " " << rem << endl;
    if (sum % 9 == 0)
    {
        py;
        return;
    }
    if (cnt2 == 0 && cnt3 == 0)
    {
        pn;
        return;
    }
    if (cnt2 >= 2 && cnt3 >= 2)
    {
        py;
        return;
    }
    if (cnt2 == 1 && cnt3 >= 1)
    {
        int tt = sum;
        if ((tt + 2) % 9 == 0)
        {
            py;
            return;
        }
        for (int i = 0; i < cnt3; i++)
        {
            tt += 6;
            if (tt % 9 == 0 || (tt + 2) % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    if (cnt2 >= 1 && cnt3 == 1)
    {
        int tt = sum;
        if ((tt + 6) % 9 == 0)
        {
            py;
            return;
        }
        for (int i = 0; i < cnt2; i++)
        {
            tt += 2;
            if (tt % 9 == 0 || (tt + 6) % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    if (cnt2 == 0)
    {
        int tt = sum;
        for (int i = 0; i < cnt3; i++)
        {
            tt += 6;
            if (tt % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    if (cnt3 == 0)
    {
        int tt = sum;
        for (int i = 0; i < cnt2; i++)
        {
            tt += 2;
            if (tt % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    cout << "hii" << endl;
    return;
}

int32_t main()
{
    init_code();
    fast_io;
    ll t = 1;
    cin >> t;
    while (t--)
        ramram();
    return 0;
}

Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>
using namespace std;

void solve()
{
    string s;
    cin >> s;

    int n = s.length();
    int que = 0;
    int cnt[2] = {0};
    for (int i = 0; i < n; ++i)
    {
        if (s[i] > '1' && s[i] < '4')
            ++cnt[s[i] - '2'];
        que += s[i] - '0';
    }

    if (que % 9 == 0)
    {
        cout << "YES" << endl;
        return;
    }

    if (cnt[0] != 0 || cnt[1] != 0)
    {
        if (cnt[0] < 2)
        {
            while (cnt[1] >= 0)
            {
                int tmp = que + cnt[1]-- * 6;
                if (tmp % 9 == 0 || (tmp + cnt[0] * 2) % 9 == 0)
                {
                    cout << "YES" << endl;
                    return;
                }
            }
        }
        else
        {
            for (int i = 0; i <= cnt[1]; ++i)
            {
                int tmp = que + i * 6;
                if (tmp % 9 == 0 || (tmp + 2) % 9 == 0 || (tmp + 4) % 9 == 0)
                {
                    cout << "YES" << endl;
                    return;
                }
            }
            que += cnt[1] * 6;
            for (int i = 3; i <= cnt[0]; ++i)
            {
                if ((que + i * 2) % 9 == 0)
                {
                    cout << "YES" << endl;
                    return;
                }
            }
        }
    }

    cout << "NO" << endl;
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}