Smallest multiple of N with exactly N digits in its Binary number representation
Given a positive integer N, the task is to find the smallest multiple of N with exactly N digits in its binary number representation.
Input: N = 3
6 is the smallest multiple of 3 and has length also 3(110) in binary.
Input: N = 5
6 is the smallest multiple of 5 and has length also 5(10100) in binary.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Approach: The idea is to make an observation.
- If we observe carefully a series will be formed as 1, 2, 6, 8, 20, …
- The N-th term in the series would be:
- Therefore, the number N is taken as the input and the above formula is implemented.
Below is the implementation of the above approach:
Time Complexity: O(n)
Auxiliary Space: O(1)