Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

A number is called k -good if it contains the digits from 0 to k at least once. Given an…

  1. User

    A number is called k -good if it contains the digits from 0 to k at least once.

    Given an array a1,a2,…,an . Count the number of k -good numbers in the array.

    Input The first line contains integers n and k (1≤n≤100 , 0≤k≤9 ). The i -th of the following n lines contains integer ai without leading zeroes (1≤ai≤109 ).

    Output Print a single integer — the number of k -good numbers in a .

    Examples inputCopy 10 6 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 outputCopy 10 inputCopy 2 1 1 10 outputCopy

    1. Solve it in c language
  2. Assistant

    Here is a possible implementation in C language:

    #include <stdio.h>
    #include <stdbool.h>
    
    bool isKGoodNumber(int num, int k) {
        bool digits[k+1];
        for (int i = 0; i <= k; i++) {
            digits[i] = false;
        }
    
        while (num > 0) {
            digits[num % 10] = true;
            num /= 10;
        }
    
        for (int i = 0; i <= k; i++) {
            if (!digits[i]) {
                return false;
            }
        }
    
        return true;
    }
    
    int main() {
        int n, k;
        scanf("%d %d", &n, &k);
    
        int count = 0;
        for (int i = 0; i < n; i++) {
            int num;
            scanf("%d", &num);
            if (isKGoodNumber(num, k)) {
                count++;
            }
        }
    
        printf("%d\n", count);
    
        return 0;
    }
    

    The isKGoodNumber function checks if a given number is a k-good number. It initializes an array digits of size k+1 to false. Then, it iteratively sets the corresponding index in the digits array to true for each digit in the given number. Finally, it checks if all digits from 0 to k have been encountered in the number.

    In the main function, we read the values of n and k from input and iterate through the array of numbers. For each number, we use the isKGoodNumber function to determine if it is a k-good number. If it is, we increment the count. Finally, we print the count of k-good numbers.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.