LeetCode solutions

1064. Smallest Integer Divisible by K

My accepted Python solution to LeetCode problem 1064, Smallest Integer Divisible by K, running in 11ms.

  • Difficulty: Medium
  • Python
  • Runtime 11ms
  • Memory 17.6MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 11ms, memory 17.6MB, accepted 2026-01-02.

python
class Solution:
    def smallestRepunitDivByK(self, k: int) -> int:
        # If k is even or divisible by 5, no solution exists
        # (111...1 is always odd and never ends in 0 or 5)
        if k % 2 == 0 or k % 5 == 0:
            return -1
        
        # Use modular arithmetic
        remainder = 0
        for length in range(1, k + 1):
            remainder = (remainder * 10 + 1) % k
            if remainder == 0:
                return length
        
        return -1

Source