Smallest Integer Divisible by K

Given a positive integer k, find the length of the smallest positive integer n such that n is divisible by k and n contains only the digit 1. Return that length, or -1 if no such n exists. Note that n may not fit in a 64-bit signed integer.

30:00

Smallest Integer Divisible by K
medium
Free written solution
Topics
Companies

Given a positive integer k, find the length of the smallest positive integer n such that n is divisible by k and n contains only the digit 1. Return that length, or -1 if no such n exists. Note that n may not fit in a 64-bit signed integer.

Example 1:
Input: {"k":1}
Output: 1
Constraints:
  • 1≤k≤1051 \leq k \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"k":1}

Look for the shortest number made only of 1s (1, 11, 111, ...) that k = 1 divides. Those numbers explode in size, so track only their remainder mod k.

Current repunit

(empty)

Remainder clock (purple = already visited)

0mod 10

remainder = (remainder × 10 + 1) % 1

length
remainder
-
0
Variables
VariableValue
k1
length-
remainder0
DepthFunction Call
Stack empty
0/4