Skip to main content

Smallest All-Ones Multiple - Solution & Explanation

MediumHash TableMath8 min read
Practice this problem

Problem Statement

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 <= 105

Approach Overview

Problem 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.

Solution

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Number ConstructionO(k^2)O(k)Conceptual understanding or when big integer arithmetic is allowed and input size is very small
Simulation with Modulo + Hash SetO(n)O(n)General case and interview setting; avoids constructing huge integers

Video Solution

Smallest All-Ones Multiple | LeetCode 3790 | Weekly Contest 482 • Sanyam IIT Guwahati • 849 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Smallest All-Ones Multiple easy or hard?
Smallest All-Ones Multiple is typically considered a medium-level problem. The challenge is recognizing that constructing the full number is unnecessary and that the problem can be solved using remainder simulation and cycle detection.
Smallest All-Ones Multiple Python/Java solution
Most implementations follow the same pattern across languages. Maintain a remainder variable, repeatedly compute (remainder * 10 + 1) % n, and store remainders in a set. Python, Java, C++, Go, and TypeScript versions all run in O(n) time with O(n) additional memory.
How to solve Smallest All-Ones Multiple in O(n)?
Simulate the number using modular arithmetic. Start with remainder = 1 % n, then repeatedly compute remainder = (remainder * 10 + 1) % n while tracking visited remainders in a hash set. When the remainder becomes 0, the constructed sequence of ones forms a multiple of n. If a remainder repeats, the process cycles and no valid number exists.
What is the best approach for Smallest All-Ones Multiple?
The optimal approach simulates the number using modulo arithmetic instead of building the full integer. Track the remainder of the current number and update it using (remainder * 10 + 1) % n. A hash set stores previously seen remainders to detect cycles. This runs in O(n) time and O(n) space.
Is Smallest All-Ones Multiple asked at Google/Amazon/Meta?
Problems involving repeated digits with modular arithmetic and remainder cycles appear in interviews at large tech companies. Variants are commonly asked at companies like Google, Amazon, and Meta to test understanding of number theory and state-cycle detection using hash sets.
What data structure is used in Smallest All-Ones Multiple?
A hash set (or hash table) stores previously seen remainders during the simulation. This prevents infinite loops and allows detection of cycles when the same remainder appears again. The rest of the algorithm relies on simple modulo math operations.
What is the time complexity of Smallest All-Ones Multiple?
The optimal algorithm runs in O(n) time because there are only n possible remainders when dividing by n. Each step generates a new remainder using (r * 10 + 1) % n and checks it in a hash set. Space complexity is also O(n) due to storing visited remainders.

Ready to solve this problem?

Practice Smallest All-Ones Multiple with our built-in code editor and test cases.

Practice on FleetCode