Count numbers up to N having digit D in its octal representation
Given a positive integer N and an integer D representing a digit, the task is to count the numbers in the range[1, N] such that at least one digit in octal representation of the number is d.
Input: N = 20, D = 7
The numbers in the range [1, 20] having digit 7 in their octal representation are 7 and 15.
Therefore, the required output is 2.
Input: N = 40, D = 5
The numbers in the range [1, 40] having digit 5 in their octal representation are 5, 13, 21, 29, 37, and 40.
Therefore, the required output is 6
Approach: Follow the steps below to solve the problem:
- Iterate over the range [1, N]. For every ith number check if at least one digit in octal representation of the number is d or not. If found to be true, then increment the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
Time Complexity: O(N * log8N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.