Length of the smallest number which is divisible by K and formed by using 1 's only | Java implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code
class GFG { static int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) { return - 1 ; } int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) { return len ; } } return - 1 ; } public static void main ( String [ ] args ) { int K = 7 ; System . out . println ( numLen ( K ) ) ; } }