Sort by

recency

|

136 Discussions

|

  • + 0 comments
    def check(n,x):
        s = ''
        for i in range(n):
            t = str(x[i])
            s += t
        s = str(s)
        p = list(permutations(s))
        for q in p:
            digit = int(''.join(q))
            if digit%3 ==0:
                return True
            else:
                return False
                
    from itertools import permutations
    for _ in range(int(input())):
        n = int(input())
        x = list(map(int, input().split()))
        print('Yes' if check(n,x) else 'No')
        
    
  • + 0 comments

    Simplest Way To Do It !! string canConstruct(vector a) { // Return "Yes" or "No" denoting whether you can construct the required number. int n = a.size(); long sum = 0;

    for(int i = 0 ; i< n; i++)
    {
        sum += a[i];
    }
    if(sum % 3== 0) return "Yes";
    else return "No";
    

    }

  • + 0 comments

    c#

    public static string canConstruct(List a)

    {
    // Return "Yes" or "No" denoting whether you can construct the required number.
        string answer = "No";
        var digits = new List<int>();
        for (int i = 0; i < a.Count; i++)
        {
            while (a[i] > 0)
            {
                digits.Add(a[i]%10);
                a[i] /= 10;
            }
        }
        int sumList = digits.Sum();
        if (sumList % 3 == 0)
        {
            answer = "Yes";
        }
        return answer;
    }
    
  • + 0 comments

    canConstruct Function:

    This function takes an array of non-negative integers (numbers) and its size (n) as parameters. It initializes an array count to count the frequency of each digit from 0 to 9. It then iterates through each number in the array, updating the digit frequencies. After counting, it calculates the sum of digits weighted by their frequencies. It returns "Yes" if the sum is divisible by 3, indicating that a new integer can be constructed, and "No" otherwise. main Function:

    It starts by inputting the number of queries (q). For each query, it takes the number of non-negative integers (n) and the integers themselves. It calls the canConstruct function for each set of numbers and prints the result. The program checks if it's possible to construct a new integer from each set of non-negative integers such that the resulting number is divisible by 3. The result for each query is printed.

  • + 0 comments

    include

    include

    include

    // Function to check if it's possible to construct a new integer divisible by m char* canConstruct(int numbers[], int n) { // Count the frequency of each digit from 0 to 9 int count[10] = {0};

    // Count the frequency of each digit in the given numbers
    for (int i = 0; i < n; i++) {
        int num = numbers[i];
        while (num > 0) {
            count[num % 10]++;
            num /= 10;
        }
    }
    
    // Check if the constructed number is divisible by 3
    int sum = 0;
    for (int i = 0; i < 10; i++) {
        sum += count[i] * i;
    }
    
    // Return "Yes" if the sum is divisible by 3, else return "No"
    return (sum % 3 == 0) ? "Yes" : "No";
    

    }

    int main() { int q; // Number of queries scanf("%d", &q);

    while (q--) {
        int n; // Number of non-negative integers
        scanf("%d", &n);
    
        int numbers[n];
    
        // Input the non-negative integers
        for (int i = 0; i < n; i++) {
            scanf("%d", &numbers[i]);
        }
    
        // Call the function to check if it's possible to construct a new integer
        printf("%s\n", canConstruct(numbers, n));
    }
    
    return 0;
    

    }