## Algorithm

Problem Name: 1015. Smallest Integer Divisible by K

Given a positive integer `k`, you need to find the length of the smallest positive integer `n` such that `n` is divisible by `k`, and `n` only contains the digit `1`.

Return the length of `n`. If there is no such `n`, return -1.

Note: `n` may not fit in a 64-bit signed integer.

Example 1:

```Input: k = 1
Output: 1
Explanation: The smallest answer is n = 1, which has length 1.
```

Example 2:

```Input: k = 2
Output: -1
Explanation: There is no such positive integer n divisible by 2.
```

Example 3:

```Input: k = 3
Output: 3
Explanation: The smallest answer is n = 111, which has length 3.
```

Constraints:

• `1 <= k <= 105`

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int smallestRepunitDivByK(int K) {
if (K % 2 == 0 || K % 5 == 0) {
return -1;
}
int remainder = 0;
for (int n = 1; n  < = K; n++) {
remainder = (remainder * 10 + 1) % K;
if (remainder == 0) {
return n;
}
}
return -1;
}
}
``````
Copy The Code &

Input

cmd
k = 1

Output

cmd
1

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const smallestRepunitDivByK = function(K) {
if (K % 2 === 0 || K % 5 === 0) return -1;
let r = 0;
for (let N = 1; N  < = K; ++N) {
r = (r * 10 + 1) % K;
if (r == 0) return N;
}
return -1;
};
``````
Copy The Code &

Input

cmd
k = 1

Output

cmd
1

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def smallestRepunitDivByK(self, K: int) -> int:
used, mod, cnt = set(), 1 % K, 1
while mod:
mod = (mod * 10 + 1) % K
cnt += 1
if mod in used:
return -1
return cnt
``````
Copy The Code &

Input

cmd
k = 3

Output

cmd
3