[LeetCode] Smallest All-Ones Multiple

3790. Smallest All-Ones Multiple

You are given a positive integer k.

Find the smallest integer n divisible by k that consists of only the digit 1 in its decimal representation (e.g., 1, 11, 111, …).

Return an integer denoting the number of digits in the decimal representation of n. If no such n exists, return -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class Solution {
public:
int minAllOneMultiple(int k) {
unordered_set<int> seen;
int rem = 1, res = 2, val = 10 % k;
while(1) {
rem = (rem + val) % k;
if(rem == 0) return res;
if(seen.count(rem)) return -1;
seen.insert(rem);
val = (val * 10) % k;
res++;
}
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/smallest-all-ones-multiple/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.