Given an integer K and an array Q[][] consisting of N queries of type {L, R}, the task for each query is to print the count of numbers from the range [L, R] that doesn’t contain the digit K in their decimal representation or octal representation.
Examples:
Input: K = 7, Q[][] = {{1, 15}}
Output: 13
Explanation:
Only number 7 and 15 contains digit K in its octal or decimal representations.
Octal representation of number 7 is 7.
Octal representation of number 15 is 17.Input: K = 8, Q[][] = {{1, 10}}
Output: 9
Explanation:
Only number 8 contains digit K in its decimal representations.
Octal representation of number 8 is 10.
Naive Approach: The idea is to traverse the range [L, R] for each query and find those numbers whose decimal and octal representations does not contain the digit K. Print the count of such numbers.
Time Complexity: O(N2*(log10(N) + log8(N)))
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to pre-compute the count of all such numbers using Prefix Sum technique. Follow the steps below to solve the problem:
- Initialize an array, say pref[], to store the count of numbers in the range [0, i] that contains digit K in their octal or decimal representation.
- Now traverse the range [0, 1e6] and perform the following steps:
- If the number contains digit K in either octal representation or decimal representation, then update pref[i] as pref[i – 1] + 1.
- Otherwise, update pref[i] as pref[i – 1].
- Traverse the given queries and print the count for each query as
Q[i][1] – Q[i][0] + 1 – (pref[Q[i][1]] – pref[Q[i][0] – 1])
Below is the implementation for the above approach :
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if the given digit // 'K' is present in the decimal and octal // representations of num or not bool contains(int num, int K, int base) { // Stores if the digit exists // or not bool isThere = 0; // Iterate till nums is non-zero while (num) { // Find the remainder int remainder = num % base; // If the remainder is K if (remainder == K) { isThere = 1; } num /= base; } return isThere; } // Function to count the numbers in the // range [1, N] such that it doesn't // contain the digit 'K' in its decimal // and octal representation void count(int n, int k, vector<vector<int> > v) { // Stores count of numbers in the // range [0, i] that contains the // digit 'K' in its octal or // decimal representation int pref[1000005] = { 0 }; // Traverse the range [0, 1e6 + 5] for (int i = 1; i < 1e6 + 5; i++) { // Check if i contains the digit // 'K' in its decimal or // octal representation bool present = contains(i, k, 10) || contains(i, k, 8); // Update pref[i] pref[i] += pref[i - 1] + present; } // Print the answer of queries for (int i = 0; i < n; ++i) { cout << v[i][1] - v[i][0] + 1 - (pref[v[i][1]] - pref[v[i][0] - 1]) << ' '; } } // Driver Code int main() { int K = 7; vector<vector<int> > Q = { { 2, 5 }, { 1, 15 } }; int N = Q.size(); // Function Call count(N, K, Q); } |
4 13
Time Complexity:O(N*(log 10(N)+log8(N)))
Auxiliary Space: O(N)
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.

