File: binary-prefix-divisible-by-5/solution.py

Date: 2026-06-06

Time: 15:23

binary-prefix-divisible-by-5/solution.py

Purpose

This file solves LeetCode 1018: Binary Prefix Divisible By 5. Given a binary array nums, it determines for each index i whether the binary number formed by nums[0..i] is divisible by 5. It's a single-responsibility module: one class, one method, one problem.

Key Components

Solution.prefixesDivBy5(self, nums: List[int]) -> List[bool] — The sole method. Takes a list of 0s and 1s representing binary digits (MSB first), returns a same-length list of booleans.

Patterns

Modular arithmetic accumulator. The key insight is that you never need to construct the actual binary number — which could grow to millions of bits. Instead, at each step you track only the remainder modulo 5:


remainder = (remainder * 2 + bit) % 5

This works because if the current prefix has value V, appending bit b gives 2V + b, and (2V + b) mod 5 = (2(V mod 5) + b) mod 5. The % 5 keeps remainder bounded to {0, 1, 2, 3, 4} regardless of input length.

This is a standard number theory technique — modular arithmetic distributes over addition and multiplication — applied here to avoid arbitrary-precision integer arithmetic.

Dependencies

Imports: Only typing.List — no external dependencies.

Imported by: The binary-prefix-divisible-by-5/test_solution.py file. The "Imported By" list in the prompt is misleading — those are other test files importing from their own solution.py, not from this one.

Flow

1. Initialize result = [] and remainder = 0.

2. For each bit in nums:

3. Return the full results list.

The loop is O(n) time, O(n) space (for the output). Each iteration does constant work — one multiply, one add, one modulo, one comparison.

Invariants

Error Handling

None. The method trusts its caller to provide a valid binary array. An empty nums returns an empty list, which is correct. Non-binary values would produce wrong results silently — acceptable for a LeetCode solution where input constraints are guaranteed by the judge.

Topics to Explore

Beliefs