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 <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach means we'll try every possible length, one by one, until we find a number that meets our requirement of being divisible by K. We'll start with the smallest possible length and keep going until we find one that works, or until we decide it's impossible.
Here's how the algorithm would work step-by-step:
def smallest_integer_divisible_by_k_brute_force(k):
length = 1
number = 1
while length <= 100000:
# Check if the current number is divisible by k
if number % k == 0:
return length
# If not, create the next number by appending a 1
number = (number * 10 + 1) % k
# This is needed so that the numbers don't get too large
# and cause issues with Python's integer size limits.
length += 1
# If we've tried a lot of numbers and haven't found one,
# it's very likely that no such number exists.
return -1The goal is to find the smallest number made of only 1s that is perfectly divisible by a given number. Instead of checking every number, we use remainders to avoid unnecessary calculations and quickly find the solution, or determine that one doesn't exist.
Here's how the algorithm would work step-by-step:
def smallest_integer_divisible_by_k(divisor):
remainder = 0
length_of_number = 0
remainders_seen = set()
while True:
length_of_number += 1
remainder = (remainder * 10 + 1) % divisor
# If remainder is 0, we found our number.
if remainder == 0:
return length_of_number
# If remainder is repeating, no solution.
if remainder in remainders_seen:
return -1
remainders_seen.add(remainder)| Case | How to Handle |
|---|---|
| K is zero | Return -1 immediately, as no number is divisible by zero. |
| K is negative | Take the absolute value of K, since divisibility applies to negative and positive integers. |
| K is 1 | Return 1 immediately, as 1 is divisible by 1. |
| Integer overflow in the remainder calculation | Use modulo operator (%) at each step to keep the remainder within the integer range. |
| No solution exists (infinite loop) | Track seen remainders and if a remainder repeats, return -1 because the search will loop indefinitely. |
| Resulting length exceeds integer limit | Return -1 if the length exceeds a reasonable limit (e.g., 100000) indicating likely infinite loop or impractical result. |
| K is a large prime number | The loop may run for a significant number of iterations before a multiple is found, so ensure the algorithm is efficient with memoization. |
| K is a power of 10 | The solution should handle this case without any specific optimization, finding the solution as expected. |