Smallest Integer Divisible by K (LeetCode 1015): Solution and Explanation
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
- If
k % 2 == 0ork % 5 == 0, return -1. - Set
remainder = 0. - For
lengthfrom 1 to k, setremainder = (remainder * 10 + 1) % k. - As soon as
remainderis 0, returnlength. - Return -1 after the loop (never reached once the first check passes).
Step-by-step example
k = 1
- k = 1 is odd and not a multiple of 5, so an answer exists.
- Start with remainder 0.
- length 1: (0 * 10 + 1) % 1 = 0. The remainder is 0, so 1 is divisible by 1.
- 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 -11class Solution {
2 public int smallestRepunitDivByK(int k) {
3 if (k % 2 == 0 || k % 5 == 0) {
4 return -1;
5 }
6 int remainder = 0;
7 for (int length = 1; length <= k; length++) {
8 remainder = (remainder * 10 + 1) % k;
9 if (remainder == 0) {
10 return length;
11 }
12 }
13 return -1;
14 }
15}1class Solution {
2public:
3 int smallestRepunitDivByK(int k) {
4 if (k % 2 == 0 || k % 5 == 0) {
5 return -1;
6 }
7 int remainder = 0;
8 for (int length = 1; length <= k; length++) {
9 remainder = (remainder * 10 + 1) % k;
10 if (remainder == 0) {
11 return length;
12 }
13 }
14 return -1;
15 }
16};Time and space complexity
O(k)
The loop runs at most k times, each with constant work.
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.