#3790
Medium Algorithms Smallest all ones multiple
Hash Table Math
46.9% acceptance
Feb 25, 2026
93
6
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn min_all_one_multiple(k: i32) -> i32 {
if k % 2 == 0 || k % 5 == 0 { return -1; }
let mut r = 0i64;
let k64 = k as i64;
for len in 1.. {
r = (r * 10 + 1) % k64;
if r == 0 { return len; }
}
-1
}
}