Solution with explanation
Why using remainder again and again?\n\t let a be some number\n\tand a % k = rem  \n\tnow when to the next number since rem != 0 we have \n\tb = 10 * a + 1\n\ta
*  **Why using remainder again and again?**\n\t let **a** be some number\n\tand **a % k = rem**  \n\tnow when to the next number since rem != 0 we have \n\t**b = 10 * a + 1**\n\tand \n\t**b % K = (10 * a + 1)%k**\n\tNow,\n\t **(10 * a + 1)%k**\n\t**= (10 % k) * (a % k) + 1 % k**\n\t**= rem * (10 % k) + 1 % K**\n\tsince [ **rem < k** ] so, **[rem  = rem % k**]\n\t**= (rem % k) * (10 % K) + 1 % k**\n\t**= (rem * 10) % K + 1 % 10**\n\t**= (rem * 10 + 1) % k**\n* **Why loop is repeated only K times ?**\n\t* We are keeping track of the remainder that has come, and it is obvious that if the remainder is repeated then we do not have the number.\n\t* Now suppose we do not get any repeated remainder with each iteration then after K iteration all the remainders will have come and in the (K+1)th iteration we will see repetition and exit and return -1.\n\t* And if we see repetition in less than K repetition then also we will exit returning -1.\n\t* so in any case the loop will run at max of K times\n\t\n```\npublic class SmallestIntegerDivisibleByK {\n// Taking k length array since there can be atMax k remainders\n\tpublic int smallestRepunitDivByK(int k) {\n\t\tint[] rem = new int[k];\n\t\tint num = 1;\n\t\tint length =1;\n\t\twhile(num%k!=0 && rem[num%k]!=1){\n\t\t\tint remainder = num%k;\n\t\t\tnum = remainder * 10 + 1;\n\t\t\tlength++;\n\t\t\trem[remainder] = 1;\n\n\t\t}\n\t\tif(num%k == 0) return length;\n\t\telse return -1;\n\t}\n}\n\n```\n**Do upvote if you like the solution. Thanks!!**
['Math', 'Java']