Untitled

 avatar
unknown
plain_text
5 months ago
879 B
4
Indexable
#include <stdio.h>
#include <math.h>
int length(int);
int length(int str) {
    int c = 0;
    while (str != 0) {
        c++;
        str /= 10;
    }
    return c;
}

int main() {
    int t;
    scanf("%d", &t);

    while (t--) {
        int str;
        scanf("%d", &str);

        int c1 = 0, c2 = 0;
        int n = length(str);  //returns length of number
        while (n > 0) {
            int digit = str / (int)pow(10, n - 1);  

            //Count occurrences of '1'
            if (digit == 1) c1++;
            else c2++;

            //Update new str 
            str %= (int)pow(10, n - 1);

            //decrese n
            n--;
        }

        // Check if the number has exactly one 
        if (c1 == 1 || c2 == 1)
            printf("YES\n");
        else
            printf("NO\n");
    }

    return 0;
}
Editor is loading...
Leave a Comment