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.
Example 1:
Input: k = 3
Output: 3
Explanation:
n = 111 because 111 is divisible by 3, but 1 and 11 are not. The length of n = 111 is 3.
Example 2:
Input: k = 7
Output: 6
Explanation:
n = 111111. The length of n = 111111 is 6.
Example 3:
Input: k = 2
Output: -1
Explanation:
There does not exist a valid n that is a multiple of 2.
Constraints:
2 <= k <= 105Problem Overview: Given an integer n, find the smallest positive integer composed only of the digit 1 (like 1, 11, 111, ...) that is divisible by n. The result can grow extremely large, so constructing the full integer directly quickly becomes impractical.
Approach 1: Brute Force Number Construction (Large Integer Simulation) (Time: O(k^2), Space: O(k))
The most straightforward idea is to repeatedly build numbers consisting only of 1. Start with 1, then 11, then 111, and check divisibility by n each time. This works in theory but fails in practice because the number grows exponentially in digits. Standard integer types overflow quickly, forcing the use of big integers or strings. Each divisibility check becomes expensive as the number length grows, resulting in roughly O(k^2) work where k is the length of the resulting number. This approach mainly helps build intuition but is not suitable for large inputs.
Approach 2: Simulation with Modulo Operation + Remainder Set (Time: O(n), Space: O(n))
The key observation: you never need the full number. Only the remainder modulo n matters. Suppose the current number of repeated ones has remainder r. Appending another 1 forms a new number whose remainder is (r * 10 + 1) % n. By repeatedly applying this transition, you simulate growing numbers while only storing remainders.
Track visited remainders using a hash table. If remainder 0 appears, the constructed sequence of ones is divisible by n. If a remainder repeats, the process entered a cycle and no such number exists. This happens when n has factors 2 or 5, since numbers composed only of 1 cannot end with even digits or 5. The algorithm effectively explores at most n unique remainders, giving O(n) time complexity.
This technique is common in problems involving repeated digit construction and modular arithmetic. The core idea comes from math properties of remainders and cycle detection.
Recommended for interviews: The modulo simulation with a remainder set is the expected solution. Interviewers want to see that you avoid constructing huge integers and instead reason about remainders. Mentioning the brute force idea first shows problem understanding, while switching to the remainder-based approach demonstrates algorithmic maturity and familiarity with hash table based cycle detection.
First, if k is even, there is no valid n that satisfies the condition, so we directly return -1.
Next, we can simulate the process of constructing an all-ones number n while taking the modulo with k to determine whether a valid n exists.
We loop k times to check whether there exists an all-ones number n divisible by k within these k iterations. In each iteration, we multiply the current remainder by 10, add 1, and then take the modulo with k. If the remainder becomes 0 in some iteration, it means we have found a valid n, and we return the current iteration count (i.e., the number of digits in the all-ones number). If no valid n is found after the loop ends, we return -1.
The time complexity is O(k), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Number Construction | O(k^2) | O(k) | Conceptual understanding or when big integer arithmetic is allowed and input size is very small |
| Simulation with Modulo + Hash Set | O(n) | O(n) | General case and interview setting; avoids constructing huge integers |
Smallest All-Ones Multiple | LeetCode 3790 | Weekly Contest 482 • Sanyam IIT Guwahati • 818 views views
Watch 3 more video solutions →Practice Smallest All-Ones Multiple with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor