|
| 1 | +# [Problem 1015: Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I need the smallest positive integer n made only of digit '1' that is divisible by k, and return its length. Directly constructing the integer will quickly overflow, so I should work with remainders modulo k. If I build numbers 1, 11, 111, ... I can update the remainder r <- (r*10 + 1) % k each step and check when r == 0. |
| 5 | + |
| 6 | +I recall that if k is divisible by 2 or 5, such a number can't exist because a decimal number composed only of 1s is odd (not divisible by 2) and its last digit is 1 (not divisible by 5). Also by pigeonhole principle, remainders repeat within k steps, so I only need to try at most k times; if I don't hit remainder 0 within k iterations it's impossible. |
| 7 | + |
| 8 | +So algorithm: handle k%2==0 or k%5==0 -> -1; then iterate up to k times computing remainders, return index when remainder zero. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +Edge cases: |
| 12 | +- k = 1 -> answer 1 (first remainder 1%1 == 0). |
| 13 | +- Very large k up to 1e5; O(k) loop is fine. |
| 14 | +- No extra memory needed beyond a few ints. |
| 15 | + |
| 16 | +Alternative detection of impossibility: instead of special-casing 2 and 5, I could run the loop and rely on the k-iteration bound; but short-circuiting for 2 and 5 is a quick check and common observation. |
| 17 | + |
| 18 | +Time complexity O(k), space O(1). Implementation straightforward in Python. |
| 19 | + |
| 20 | +## Attempted solution(s) |
| 21 | +```python |
| 22 | +class Solution: |
| 23 | + def smallestRepunitDivByK(self, k: int) -> int: |
| 24 | + """ |
| 25 | + Return the length of the smallest positive integer consisting only of '1's |
| 26 | + that is divisible by k, or -1 if none exists. |
| 27 | + """ |
| 28 | + # Quick impossibility check: any number consisting only of 1's is odd and |
| 29 | + # ends with digit 1, so it cannot be divisible by 2 or 5. |
| 30 | + if k % 2 == 0 or k % 5 == 0: |
| 31 | + return -1 |
| 32 | + |
| 33 | + remainder = 0 |
| 34 | + # Try lengths from 1 to k (pigeonhole: if no remainder 0 in k steps, it won't appear) |
| 35 | + for length in range(1, k + 1): |
| 36 | + remainder = (remainder * 10 + 1) % k |
| 37 | + if remainder == 0: |
| 38 | + return length |
| 39 | + return -1 |
| 40 | +``` |
| 41 | +- Notes: |
| 42 | + - Approach: iterate building the remainder of 1, 11, 111, ... modulo k using remainder = (remainder*10 + 1) % k. Return the first length where remainder == 0. |
| 43 | + - Time complexity: O(k) in the worst case (at most k iterations). |
| 44 | + - Space complexity: O(1) extra space. |
| 45 | + - Important implementation details: avoid constructing the large integer itself; only track remainder. Early return for k divisible by 2 or 5 to handle impossibility quickly. |
0 commit comments