C++ || CLEAN CODE || WITH PROPER EXPLANATION
class Solution {\npublic:\n    int smallestRepunitDivByK(int k) {\n        \n        /\n        \n We have to solve the question using two key problems ans ther
class Solution {\npublic:\n    int smallestRepunitDivByK(int k) {\n        \n        /*\n        \n We have to solve the question using two key problems ans there solutions .........\n        \n    Problem....(a) As n may not fit in a 64 bit signed integer.\n    \n    Problem....(b) There is no info about the maximum length of n until which we should check for.\n    \n    \n    Solution....(a) We have to use the property i.e A number X is divisible by K if and only if sum of remainders all the digits of X by K is also  divisible by K .\n    \n    Example (1): X=111 is divisible by K=3 \n    \n    digits of 111 : 1,1,1  now (1%3)=1 , (1%3)=1 ,(1%3)=1\n    \n    sum of those remainders (1+1+1)=3 which is divisible by K=3\n    \n    \n    Solution....(b) Notice that if N does not exist, Our codes\'s (See below) while-loop will continue endlessly. However, the possible values of remainder are limited -- ranging from 0 to K-1. Therefore, if the while-loop continues forever, the remainder repeats after every certain period . Also, if remainder repeats, then it gets into a loop. Hence, the while-loop is endless if and only if the remainder repeats.\n    \n    As  rem=(X%K)   and rem should be lie between [0,K-1] .\n    \n         */\n        \n        int i=0,n=0;\n        \n        while(i++<=k){\n            \n            n=(n*10+1)%k;\n            \n            if(n==0){\n                return i;\n            }\n            \n        }\n        \n       return -1;\n        \n    }\n};
[]