Smallest Integer Divisible by K (LeetCode 1015): Solution and Explanation

Medium
Math
Hash Table

Given a positive integer k, return the length of the smallest number made only of the digit 1 that is divisible by k, or -1 if none exists.

Intuition

The candidates are 1, 11, 111, and so on. They grow by one digit each time, so after a few dozen steps they no longer fit in any integer type.

Divisibility only depends on the remainder mod k. Going from N to the next candidate means N * 10 + 1, so the remainder goes from r to (r * 10 + 1) % k. That is all the state needed.

Every candidate ends in 1, so it is odd and not a multiple of 5. If k is divisible by 2 or 5, no candidate can work and the answer is -1.

Otherwise the remainders live in a set of only k values. The step r to (10r + 1) % k is reversible when k shares no factor with 10, so the sequence is a pure cycle starting at 0, and it must come back to 0 within k steps.

Approach: Remainder walk

  1. If k % 2 == 0 or k % 5 == 0, return -1.
  2. Set remainder = 0.
  3. For length from 1 to k, set remainder = (remainder * 10 + 1) % k.
  4. As soon as remainder is 0, return length.
  5. Return -1 after the loop (never reached once the first check passes).

Step-by-step example

k = 1
  1. k = 1 is odd and not a multiple of 5, so an answer exists.
  2. Start with remainder 0.
  3. length 1: (0 * 10 + 1) % 1 = 0. The remainder is 0, so 1 is divisible by 1.
  4. Return 1.

Code: Python, Java, C++

1class Solution:
2    def smallestRepunitDivByK(self, k: int) -> int:
3        if k % 2 == 0 or k % 5 == 0:
4            return -1
5        remainder = 0
6        for length in range(1, k + 1):
7            remainder = (remainder * 10 + 1) % k
8            if remainder == 0:
9                return length
10        return -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(k)

The loop runs at most k times, each with constant work.

Space

O(1)

Only the current remainder and the length are stored.

Other approaches

Remainder set for cycle detection (time O(k), space O(k))

Skip the 2 and 5 check and instead store every remainder seen in a hash set. If a remainder repeats before 0 appears, return -1. Correct, but it uses O(k) memory for something a one-line check decides.

Big integers (time O(k^2), space O(k))

Build 1, 11, 111, ... as arbitrary-precision numbers and test each one. Each step costs time proportional to the number of digits, so it is O(k^2) overall and slow for large k.

Common mistakes

  • Building the repunit itself in a 64-bit integer. It overflows after 19 digits, long before the answer for most k.
  • Looping forever when k is divisible by 2 or 5. Either check that first or cap the loop at k iterations.
  • Starting the remainder at 1 and the length at 0, or similar off-by-one mistakes. The first candidate, 1, has length 1.
  • Computing (remainder * 10 + 1) without taking % k each step, which lets the value grow and overflow.

Frequently asked questions

Why is the answer at most k?

There are only k remainders. When k shares no factor with 10, the map r to (10r + 1) % k is one-to-one, so the remainder sequence starting from 0 is a cycle that returns to 0. A cycle over at most k values has length at most k.

Why does a factor of 2 or 5 make it impossible?

A multiple of k is then a multiple of 2 or 5, so its last digit is even or 0 or 5. Every repunit ends in 1, so none qualifies.

Is the answer related to the multiplicative order of 10?

Yes. The repunit of length L equals (10^L - 1) / 9, so for k not divisible by 3 the answer is the order of 10 mod k. Multiples of 3 need extra care because of the division by 9, which is why the direct remainder walk is simpler.