0 ms C++ solution with explanation
Explanation:\nFirst, you should be able to convince yourself that if K is even or divisible by 5, then no multiple of K can end in 1.\nWhat\'s perhaps not obvio
Explanation:\nFirst, you should be able to convince yourself that if K is even or divisible by 5, then no multiple of K can end in 1.\nWhat\'s perhaps not obvious is that if K is not even or divisible by 5, then there must be a solution.  Here\'s a nice way to see it:\n\nLet n_1 = 1, n_2 = 11, n_3 = 111, etc.  Note that n_i is a multiple of K if and only if  ( n_i % K == 0 ) .\n\nAs we look at all the terms in this sequence taken modulo K, since the sequence is infinitely long but there are only finitely many residue classes mod K, we must eventually repeat some value.  Say,  (n_i % K  == n_j % K ), where i and j are distinct positive integers.\n\nThen (n_i - n_j % K == 0).  But if you look at these numbers, you get something like (11111111-111 % K == 0), i.e. (11111000 % K == 0).  Since we\'ve already checked that K has no factor of 2 or 5, this implies that (11111 % K == 0).\n\nI want to make sure you don\'t confuse this logic with the code below.  This reasoning is only to justify that there is some solution.  In the actual code, we don\'t need to carry out this same logic.  We can just look for the first n_i whose value mod K is 0, and because of the argument above we know that this search will terminate (in fact, we know it will terminate after K or fewer steps, since it will take at most K+1 steps to repeat a value mod K).\n\n```\nclass Solution {\npublic:\n    int smallestRepunitDivByK(int K) {\n        if ((K%2 == 0) or (K%5 == 0))\n            return -1;\n        \n        int currentLength = 1;\n        int currentModulus = currentLength % K;\n        \n        while (currentModulus != 0)\n        {\n            currentModulus *= 10;\n            currentModulus += 1;\n            currentModulus = currentModulus % K;\n            currentLength += 1;                      \n        }\n        return currentLength;        \n    }\n};\n```
[]