All problems
1106MediumHash TableMathPigeonhole Principle

Stamping Ones Until It Divides

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1015Smallest Integer Divisible by K

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A machine stamps out numbers written with the digit 1 alone: 1, then 11, then 111, and so on without end.

Return the number of digits in the shortest of these that k divides exactly, or -1 when k divides none of them.

Examples

Example 1

Input
k = 7
Output
6

The five shortest stamps all leave a remainder, and the sixth is seven times 15873.

Example 2

Input
k = 5
Output
-1

Every stamp ends in a one, while a multiple of five has to end in a five or a zero, so no stamp can ever divide.

Example 3

Input
k = 9
Output
9

A multiple of nine has digits adding to a multiple of nine, so nine stamps are needed, and that number is nine times 12345679.

Constraints

  • 1 <= k <= 10^5

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def smallest_repunit_div_by_k(k: int) -> int:
Java
public int smallestRepunitDivByK(int k)
September 7
Apply