1018. Binary Prefix Divisible By 5
Difficulty: Easy
Topics: Array, Bit Manipulation, Weekly Contest 130
You are given a binary array nums (0-indexed).
We define xᵢ as the number whose binary representation is the subarray nums[0..i] (from most-significant-bit to least-significant-bit).
- For example, if
nums = [1,0,1], thenx₀ = 1,x₁ = 2, andx₂ = 5.
Return an array of booleans answer where answer[i] is true if xᵢ is divisible by 5.
Example 1:
- Input: nums = [0,1,1]
- Output: [true,false,false]
-
Explanation: The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10.
- Only the first number is divisible by 5, so answer[0] is true.
Example 2:
- Input: nums = [1,1,1]
- Output: [false,false,false]
Constraints:
1 <= nums.length <= 10⁵nums[i] is either 0 or 1.
Hint:
- If
Xis the firstidigits of the array as a binary number, then2X + A[i]is the firsti+1digits.
Solution:
We cannot directly convert the binary number to an integer due to potential overflow issues. Instead, we use modular arithmetic to efficiently compute the remainder when the number is divided by 5 at each step.
Approach
-
Modular Arithmetic: The key insight is that for any number formed by the first
ibits, the number formed by the firsti+1bits can be expressed as(current_value * 2 + next_bit). By taking the modulo 5 of this value at each step, we keep the current value manageable and within a small range (0 to 4). -
Iterative Calculation: Traverse the binary array, updating the current value using the formula
current = (current * 2 + bit) % 5. This ensures that we only track the remainder when divided by 5, which is sufficient to check divisibility. -
Check Divisibility: After updating the current value for each bit, check if the current value modulo 5 is zero. If it is, mark the position as
truein the result array; otherwise, mark it asfalse.
Let's implement this solution in PHP: 1018. Binary Prefix Divisible By 5
<?php
/**
* @param Integer[] $nums
* @return Boolean[]
*/
function prefixesDivBy5($nums) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo prefixesDivBy5([0,1,1]) . "\n"; // Output: [true,false,false]
echo prefixesDivBy5([1,1,1]) . "\n"; // Output: [false,false,false]
?>
Explanation:
-
Initialization: Start with
current = 0, which represents the initial value of the binary number formed. -
Processing Each Bit: For each bit in the array, update
currentusing the formula(current * 2 + bit) % 5. This formula effectively builds the binary number step by step while keeping the value within bounds by leveraging modulo operation. -
Result Construction: For each updated
currentvalue, check if it is zero (indicating divisibility by 5) and store the result in the output array.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)