LeetCode Entry

1018. Binary Prefix Divisible By 5

24.11.2025 easy 2025 kotlin rust

Prefixes divisible by 5

1018. Binary Prefix Divisible By 5 easy blog post substack youtube

b21bafa5-e591-49e3-8c72-f8752080b3a0 (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1183

Problem TLDR

Prefixes divisible by 5 #easy #brainteaser

Intuition

    // its not easy; its brainteaser
    // the 10^5 length
    // how to convert base_2 to base_5 directly?
    // to convert to base_10
    // let's try convert to long base_10
    // it goes out of range too fast
    // can we trim it % 5?

Approach

  • the remainder of %5 is enough

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

// 3ms
    fun prefixesDivBy5(n: IntArray) =
    { var x = 0; n.map {x = (x*2+it)%5; x<1 }}()
// 0ms
    pub fn prefixes_div_by5(n: Vec<i32>) -> Vec<bool> {
        let mut x = 0; n.iter().map(|&n|{x=(x*2+n)%5; x<1}).collect()
    }