#1018
Easy Algorithms Binary prefix divisible by 5
Array Bit Manipulation
53.5% acceptance
Feb 25, 2026
1101
230
You are given a binary array nums (0-indexed).
We define xi 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], then x0 = 1, x1 = 2, and x2 = 5.
Return an array of booleans answer where answer[i] is true if xi is divisible by 5.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn prefixes_div_by5(nums: Vec<i32>) -> Vec<bool> {
let mut cur = 0;
nums.iter().map(|&b| { cur = (cur * 2 + b) % 5; cur == 0 }).collect()
}
}