09 Sept 2026Go / Python / TypeScriptEasy

Binary Prefix Divisible By 5

Report whether each binary-array prefix represents a number divisible by five.

Update only the prefix remainder modulo five as each bit arrives; divisibility depends on that remainder, not the full potentially large number.

complexity

O(n) time and O(n) output space.

solution files

  • Go binary-prefix-divisible-by-5/solution.go
  • Python binary-prefix-divisible-by-5/solution.py
  • TypeScript binary-prefix-divisible-by-5/solution.ts

Solution files

Gobinary-prefix-divisible-by-5/solution.go
package main

func prefixesDivBy5(nums []int) []bool {
	result := make([]bool, len(nums))
	remainder := 0
	for index, bit := range nums {
		remainder = (remainder*2 + bit) % 5
		result[index] = remainder == 0
	}
	return result
}
Pythonbinary-prefix-divisible-by-5/solution.py
class Solution:
    def prefixesDivBy5(self, nums: list[int]) -> list[bool]:
        result: list[bool] = []; remainder = class="syntax-number">0
        for bit in nums:
            remainder = (remainder * class="syntax-number">2 + bit) % class="syntax-number">5
            result.append(remainder == class="syntax-number">0)
        return result
TypeScriptbinary-prefix-divisible-by-5/solution.ts
function prefixesDivBy5(nums: number[]): boolean[] {
  const result: boolean[] = []; let remainder = class="syntax-number">0;
  for (const bit of nums) { remainder = (remainder * class="syntax-number">2 + bit) % class="syntax-number">5; result.push(remainder === class="syntax-number">0); }
  return result;
}