Binary Prefix Divisible By 5 (LeetCode 1018): Solution and Explanation

Easy
Array
Bit Manipulation

Read a binary array one bit at a time, most significant bit first, and report after every bit whether the number read so far is a multiple of 5. With nums = [0,1,1] the prefixes are 0, 1 and 3, so the answer is [true,false,false].

Intuition

The prefix numbers can be 100000 bits long, which no built-in integer type holds. Building them directly is either an overflow bug or, in Python, a quadratic slowdown.

Reading one more bit turns the prefix x into 2x + bit. That is the only operation ever applied to the number.

Divisibility by 5 only depends on x % 5, and (2x + bit) % 5 can be computed from x % 5 alone. So the remainder is all that needs to be carried forward.

That makes the whole problem a tiny machine with 5 states, the remainders 0 to 4. Each bit moves it along one arrow, and the answer for that prefix is whether it landed on state 0.

Approach: Running remainder

  1. Start with rem = 0.
  2. For each bit, set rem = (2 * rem + bit) % 5, written here as ((rem << 1) | bit) % 5.
  3. Append rem == 0 to the answer.
  4. Return the answer.

Step-by-step example

nums = [1,0,1,0,0]
  1. Bit 1: rem = (0 * 2 + 1) % 5 = 1, the prefix is 1, so false.
  2. Bit 0: rem = (1 * 2 + 0) % 5 = 2, the prefix is 2, so false.
  3. Bit 1: rem = (2 * 2 + 1) % 5 = 0, the prefix is 5, so true.
  4. Bit 0: rem = (0 * 2 + 0) % 5 = 0, the prefix is 10, so true.
  5. Bit 0: rem stays 0, the prefix is 20, so true. The answer is [false,false,true,true,true].

Code: Python, Java, C++

1class Solution:
2    def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
3        answer = []
4        rem = 0
5        for bit in nums:
6            rem = ((rem << 1) | bit) % 5
7            answer.append(rem == 0)
8        return answer
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each bit costs one shift, one OR and one modulo.

Space

O(1)

Only the remainder is kept, apart from the output array.

Other approaches

Build the full number (time O(n^2), space O(n))

Keep the real prefix value and test it with % 5. It overflows in fixed-width languages after about 63 bits, and with big integers every step costs time proportional to the length, so the total is quadratic.

Explicit transition table (time O(n), space O(1))

Precompute next[rem][bit] for the 5 states and follow it. Same complexity, and it makes the automaton view concrete, which generalizes to any divisor.

Common mistakes

  • Keeping the whole prefix in a 32-bit or 64-bit integer. It overflows long before the end of the array.
  • Reducing only at the end. The modulo has to be taken after every bit so the value stays small.
  • Reading the bits in the wrong order. nums[0] is the most significant bit, so each new bit goes on the right.

Frequently asked questions

Why is it safe to drop everything except the remainder?

If x = 5q + r, then 2x + b = 10q + 2r + b, and 10q is a multiple of 5. So (2x + b) % 5 = (2r + b) % 5, which needs only r.

Does the same trick work for other divisors?

Yes. For any divisor k keep rem = (2 * rem + bit) % k. The machine just has k states instead of 5.