class Solution:
def prefixesDivBy5(self, A):
result = []
num = 0
for bit in A:
num = ((num << 1) + bit) % 5
result.append(num == 0)
return result
class Solution {
public:
vector<bool> prefixesDivBy5(vector<int>& A) {
vector<bool> result;
int num = 0;
for (int bit : A) {
num = ((num << 1) + bit) % 5;
result.push_back(num == 0);
}
return result;
}
};
class Solution {
public List<Boolean> prefixesDivBy5(int[] A) {
List<Boolean> result = new ArrayList<>();
int num = 0;
for (int bit : A) {
num = ((num << 1) + bit) % 5;
result.add(num == 0);
}
return result;
}
}
var prefixesDivBy5 = function(A) {
let result = [];
let num = 0;
for (let bit of A) {
num = ((num << 1) + bit) % 5;
result.push(num === 0);
}
return result;
};
Given an array A
of 0s and 1s, each representing a single bit of a binary number, you are to return a list of booleans. For each prefix of the array (from the start up to each index), determine whether the binary number represented by that prefix is divisible by 5. The output list should have the same length as A
, where each element at index i
is True
if the binary number formed by A[0]
to A[i]
is divisible by 5, otherwise False
.
Constraints:
A
is either 0
or 1
.A
is between 1 and 30000.At first glance, the problem seems to ask for repeated conversion of binary arrays to integers, and then checking divisibility by 5. However, converting each prefix to an integer for every index would be inefficient, especially for long arrays. We need a way to efficiently update our calculation as we extend the prefix by one bit at each step.
The key insight is that we can build the current number incrementally as we process each bit, using the mathematical property of binary numbers: appending a bit to a binary number is equivalent to multiplying the current number by 2 and adding the new bit. We can also use modular arithmetic to keep the number manageable, since we only care about divisibility by 5.
We will process the input array A
from left to right, maintaining a running total representing the current prefix as a binary number, but always keeping it modulo 5. This ensures that the numbers never get too large, and we can quickly check divisibility.
num
to 0. This will store our running binary number modulo 5.bit
in A
:num
as num = (num * 2 + bit) % 5
.True
to the result list if num
is 0 (divisible by 5), else False
.This approach is efficient because:
Let's take A = [1, 0, 1, 1, 1, 0, 0, 1, 0]
.
num = 0
.
So the output is [False, False, True, False, False, False, False, True, True]
.
A
. The space complexity is also O(N), for the result array.The problem is elegantly solved using modular arithmetic, building the binary number incrementally and always working modulo 5. This avoids large integer computations and makes the solution efficient and concise. The key insight is that for divisibility, we only need to track the remainder, not the entire number, making the solution both fast and memory efficient.