Very short and clear explanation O(K) (also 2 lines[Java/C++/C#/JavaScript/PHP]).
We\'re interested a % K == 0, for a=1,11,111,..  Next element a = (a*10+1)%K, notice that this is a function of one value that maps a value of (0 to K-1) to a v
We\'re interested a % K == 0, for a=1,11,111,..  Next element ```a = (a*10+1)%K```, notice that this is a function of one value that maps a value of (0 to K-1) to a value of (0 to K-1). So, if after K runs we haven\'t seen 0, it means we have seen another value (0,K-1) twice, which means we\'ve already are in a loop between those two occurances, and if 0 hasn\'t occured, it never will.\n\n```C++\n// C++, Java, C, C#\nfor(int i=1,a=0;i<=K;i++) if((a=(a*10+1)%K)==0)return i;\nreturn -1;\n```\n```JavaScript\n// Let it be JavaScript\nfor(let i=1,a=0;i<=K;i++) if((a=(a*10+1)%K)==0)return i;\nreturn -1;\n```\n```PHP\n// PHP $$$\nfor($i=1,$a=0;$i<=$K;$i++) if(($a=($a*10+1)%$K)==0)return $i;\nreturn -1;\n```\n```Python\n# Python is too line verbose\na = 0\nfor i in range(1,K+1):\n\ta=(a*10+1)%K\n\tif a==0:\n\t\treturn i\nreturn -1\n```
[]