Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Math
3
7
2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3790: Smallest All-Ones Multiple
class Solution {
public int minAllOneMultiple(int k) {
if ((k & 1) == 0) {
return -1;
}
int x = 1 % k;
int ans = 1;
for (int i = 0; i < k; i++) {
x = (x * 10 + 1) % k;
ans++;
if (x == 0) {
return ans;
}
}
return -1;
}
}
// Accepted solution for LeetCode #3790: Smallest All-Ones Multiple
func minAllOneMultiple(k int) int {
if k&1 == 0 {
return -1
}
x := 1 % k
ans := 1
for i := 0; i < k; i++ {
x = (x*10 + 1) % k
ans++
if x == 0 {
return ans
}
}
return -1
}
# Accepted solution for LeetCode #3790: Smallest All-Ones Multiple
class Solution:
def minAllOneMultiple(self, k: int) -> int:
if k % 2 == 0:
return -1
x = 1 % k
ans = 1
for _ in range(k):
x = (x * 10 + 1) % k
ans += 1
if x == 0:
return ans
return -1
// Accepted solution for LeetCode #3790: Smallest All-Ones Multiple
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3790: Smallest All-Ones Multiple
// class Solution {
// public int minAllOneMultiple(int k) {
// if ((k & 1) == 0) {
// return -1;
// }
//
// int x = 1 % k;
// int ans = 1;
//
// for (int i = 0; i < k; i++) {
// x = (x * 10 + 1) % k;
// ans++;
// if (x == 0) {
// return ans;
// }
// }
//
// return -1;
// }
// }
// Accepted solution for LeetCode #3790: Smallest All-Ones Multiple
function minAllOneMultiple(k: number): number {
if ((k & 1) === 0) {
return -1;
}
let x = 1 % k;
let ans = 1;
for (let i = 0; i < k; i++) {
x = (x * 10 + 1) % k;
ans++;
if (x === 0) {
return ans;
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.