Given a positive integer N whose unit’s digit is 3. Find the number of 1s in the smallest repunit which is divisible by the given number N. Every number whose unit’s digit is 3 has a repunit as its multiple. A repunit is a number which has only ones. It is of the form (10n – 1)/9.
As 3 divides 111 which is the smallest repunit
multiple of the number. So the number of ones in 111 is 3.
The repunits are 1, 11, 111, 1111, …. the next repunit to x will always be x*10+1. If the remainder left by x repunit is r then remainder left by the next repunit will always be (r*10+1)%n. Since the repunit can be very large, there is no need to find the repunit number. Simply counting the number of ones will give us the answer.
So, find out the remainders of all repunit numbers until the remainder becomes 0. Once it does, then the count of iterations done to make remainder 0 will be the number of 1’s.
Below is the implementation of above approach :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Repunit numbers
- Maximum number of ones in a N*N matrix with given constraints
- Length of longest consecutive ones by at most one swap in a Binary String
- Move all zeros to start and ones to end in an Array of random integers
- Find the smallest number whose digits multiply to a given number n
- Smallest number by rearranging digits of a given number
- Get the kth smallest number using the digits of the given number
- Immediate smallest number after re-arranging the digits of a given number
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Find smallest number with given number of digits and sum of digits under given constraints
- Smallest odd number with even sum of digits from the given number N
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find smallest number formed by inverting digits of given number N
- Given a number, find the next smallest palindrome
- Smallest number divisible by first n numbers
- Smallest number to multiply to convert floating point to natural
- Smallest number with at least n trailing zeroes in factorial
- Smallest number with at least n digits in factorial