We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// 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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Constructing a Number
You are viewing a single comment's thread. Return to all 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};
}
int main() { int q; // Number of queries scanf("%d", &q);
}