Given a number in the form of an array A of size N. Each of the digits of the number is represented by A[i]. Check if the number is divisible by 3.
1 <= N <= 10^5
0 <= A[i] <= 9
A[1] ≠ 0
Given an integer array representing the number
Return 1 if the number is divisible by 3 and return 0 otherwise.
Input 1:
A = [1, 2, 3]
Input 2:
A = [1, 0, 0, 1, 2]
Output 1:
1
Output 2:
0
Explanation 1:
The number 123 is divisible by 3.
Explanation 2:
The number 10012 is not divisible by 3.
function isDivisibleBy3(A) {
let sum = 0;
for (let i = 0; i < A.length; i++) {
sum += A[i];
}
return sum % 3 === 0 ? 1 : 0;
}
// Example usage:
console.log(isDivisibleBy3([1, 2, 3])); // Output: 1
console.log(isDivisibleBy3([1, 0, 0, 1, 2])); // Output: 0