Advertisement
1015. Smallest Integer Divisible by K
MediumView on LeetCode
Time: O(K)
Space: O(K)
Approach
Simulate repunit growth mod K; if a remainder repeats before reaching 0 it's impossible; early-exit if K ends in even digit or 5.
1015.cs
C#
// Approach: Simulate repunit growth mod K; if a remainder repeats before reaching 0 it's impossible; early-exit if K ends in even digit or 5.
// Time: O(K) Space: O(K)
public class Solution
{
public int SmallestRepunitDivByK(int k)
{
if (k % 10 != 1 && k % 10 != 3 && k % 10 != 7 && k % 10 != 9)
return -1;
HashSet<int> seen = new HashSet<int>();
int n = 0;
for (int length = 1; length <= k; ++length)
{
n = (n * 10 + 1) % k;
if (n == 0)
return length;
if (seen.Contains(n))
return -1;
seen.Add(n);
}
return -1;
}
}Advertisement
Was this solution helpful?