[C++] Simplest Intuitive Solution | Faster than 100%
APPROACH : \n We know that any such number with 1 in all digits doesn\'t exist for k=2 or k=5.\n Therefore any multiple of 2 or 5 also don\'t divide any such nu
**APPROACH :** \n* We know that any such number with ```1``` in all digits doesn\'t exist for ```k=2``` or ```k=5```.\n* Therefore any multiple of ```2``` or ```5``` also don\'t divide any such number. \n* So if ```k``` is divisible by either ```2``` or ```5```, we return ```-1```;\n* For any other ```k```, we initialize ```n=1``` and ```length=1``` (stores the length of such number) and we loop (we do ```n=n*10+1``` & ```length++``` )until we find such number that\'s divisible by ```k``` (```n%k == 0```)\n* But soon, it will cause integer overflow if we keep storing ```n```. So instead we store the remainder that\'s obtained when we divide ```n``` with ```k```. Why? \n\n**Why do we store the remainders modulo ```k``` ?**\nThe reason is simple : \n* For every iteration, ```n = kQ + r``` , for some quotient ```Q``` and remainder ```r```.\n* The above equation can be written as : ```(n*10 + 1) = (kQ + r)*10 + 1```\n* So ```n*10 + 1 = kQ*10 + (r*10 + 1)```. \n* ```kQ*10``` is divisible by ```k```. So for ```n*10 + 1``` to be divisible by ```k```, ```r*10 + 1``` should be divisible by ```k```. \n\n**Time Complexity :** O(k) \n\n**Space Complexity :** O(1)\n\n**Code :**\n```\nclass Solution {\npublic:\n    int smallestRepunitDivByK(int k) {\n        if(!(k%2) || !(k%5)) return -1;\n        \n        auto r = 1, length = 1;\n        while(true){\n            r = r%k;\n            if(!r) return length;\n            r = r*10 + 1;\n            length++;\n        }\n    }\n};\n```\n\n\n
['C', 'C++']