Count numbers that does not contain digit N in given range
Last Updated :
23 Jul, 2025
Given integers, N, L, and R, the task is to find the number of integers in the range L to R that does not contain the digit N. print the answer modulo 109 + 7. ( L ? R ? 101000000)
Examples:
Input: N = 5, L = 1, R = 10
Output: 9
Explanation: excluding all 5 others from 1 to 10 will be included in the answer.
Input: N = 5, L = 1, R = 100
Output: 81
Explanation: Excluding 5, 15, 25, 35, 45, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 65, 75, 85, and 95 all numbers from 1 to 100 will be included in the answer
Naive approach: The basic way to solve the problem is as follows:
The basic way to solve this problem is to generate all possible combinations by using a recursive approach.
Time Complexity: O(18N), Where N is the number of digits to be filled.
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following idea:
Dynamic programming can be used to solve this problem
- dp[i][j] represents numbers in the range with i digits and j represents tight condition.
- It can be observed that the recursive function is called exponential times. That means that some states are called repeatedly.
- So the idea is to store the value of each state. This can be done using by store the value of a state and whenever the function is called, return the stored value without computing again.
- First answer will be calculated for 0 to A - 1 and then calculated for 0 to B then latter one is subtracted with prior one to get answer for range [L, R]
Follow the steps below to solve the problem:
- Create a recursive function that takes two parameters i representing the position to be filled and j representing the tight condition.
- Call the recursive function for choosing all digits from 0 to 9 apart from N.
- Base case if size digit formed return 1;
- Create a 2d array dp[N][2] initially filled with -1.
- If the answer for a particular state is computed then save it in dp[i][j].
- If the answer for a particular state is already computed then just return dp[i][j].
Below is the implementation of the above approach:
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
// dp table initialized with -1
int dp[100001][2];
// Recursive Function to find numbers
// in the range L to R such that they
// do not contain digit N
int recur(int i, int j, int N, string& a)
{
// Base case
if (i == a.size()) {
return 1;
}
// If answer for current state is already
// calculated then just return dp[i][j]
if (dp[i][j] != -1)
return dp[i][j];
// Answer initialized with zero
int ans = 0;
// Tight condition true
if (j == 1) {
// Iterating from 0 to max value
// of tight condition
cout<<((int)a[i] - 48)<<endl;
for (int k = 0; k <= ((int)a[i] - 48); k++) {
// N is not allowed to use
if (k == N)
continue;
// When k is at max tight condition
// remains even in next state
if (k == ((int)a[i] - 48))
// Calling recursive function
// for tight digit
ans += recur(i + 1, 1, N, a);
// Tight condition drops
else
// Calling recursive function
// for digits less than tight
// condition digit
ans += recur(i + 1, 0, N, a);
}
}
// Tight condition false
else {
// Iterating for all digits
for (int k = 0; k <= 9; k++) {
// Digit N is not possible
if (k == N)
continue;
// Calling recursive function for
// all digits from 0 to 9
ans += recur(i + 1, 0, N, a);
}
}
// Save and return dp value
return dp[i][j] = ans;
}
// Function to find numbers
// in the range L to R such that they
// do not contain digit N
int countInRange(int N, int A, int B)
{
// Initializing dp array with - 1
memset(dp, -1, sizeof(dp));
A--;
string L = to_string(A), R = to_string(B);
// Numbers with sum of digits T from
// 1 to L - 1
int ans1 = recur(0, 1, N, L);
// Initializing dp array with - 1
memset(dp, -1, sizeof(dp));
// Numbers with sum of digits T in the
// range 1 to R
int ans2 = recur(0, 1, N, R);
// Difference of ans2 and ans1
// will generate answer for required
// range
return ans2 - ans1;
}
// Driver Code
int main()
{
// Input 1
int N = 5, L = 1, R = 10;
// Function Call
cout << countInRange(N, L, R) << endl;
// Input 2
//int N1 = 5, L1 = 1, R1 = 100;
// Function Call
//cout << countInRange(N1, L1, R1) << endl;
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
class GFG {
static final int MOD = 1_000_000_007;
// dp table initialized with -1
static int[][] dp = new int[100001][2];
// Recursive Function to find numbers
// in the range L to R such that they
// do not contain digit N
static int recur(int i, int j, int N, String a)
{
// Base case
if (i == a.length()) {
return 1;
}
// If answer for current state is already
// calculated then just return dp[i][j]
if (dp[i][j] != -1)
return dp[i][j];
// Answer initialized with zero
int ans = 0;
// Tight condition true
if (j == 1) {
// Iterating from 0 to max value
// of tight condition
for (int k = 0; k <= a.charAt(i) - '0'; k++) {
// N is not allowed to use
if (k == N)
continue;
// When k is at max tight condition
// remains even in next state
if (k == a.charAt(i) - '0')
// Calling recursive function
// for tight digit
ans += recur(i + 1, 1, N, a);
// Tight condition drops
else
ans += recur(i + 1, 0, N, a);
}
}
// Tight condition false
else {
// Iterating for all digits
for (int k = 0; k <= 9; k++) {
// Digit N is not possible
if (k == N)
continue;
// Calling recursive function for
// all digits from 0 to 9
ans += recur(i + 1, 0, N, a);
}
}
// Save and return dp value
return dp[i][j] = ans;
}
// Function to find numbers
// in the range L to R such that they
// do not contain digit N
static int countInRange(int N, int A, int B)
{
// Initializing dp array with - 1
for (int[] row : dp) {
Arrays.fill(row, -1);
}
A--;
String L = Integer.toString(A);
String R = Integer.toString(B);
// Numbers with sum of digits T from
// 1 to L - 1
int ans1 = recur(0, 1, N, L);
// Initializing dp array with - 1
for (int[] row : dp) {
Arrays.fill(row, -1);
}
// Numbers with sum of digits T in the
// range 1 to R
int ans2 = recur(0, 1, N, R);
// Difference of ans2 and ans1
// will generate answer for required
// range
return ans2 - ans1;
}
public static void main(String[] args)
{
// Input 1
int N = 5;
int L = 1;
int R = 10;
// Function Call
System.out.println(countInRange(N, L, R));
// Input 2
int N1 = 5;
int L1 = 1;
int R1 = 100;
// Function Call
System.out.println(countInRange(N1, L1, R1));
}
}
// This contributed by lokeshmvs21.
Python3
# Python code to implement the approach
MOD = 1e9 + 7;
# dp table initialized with -1
dp= [[-1]*(2) for _ in range(100001)];
# Recursive Function to find numbers
# in the range L to R such that they
# do not contain digit N
def recur(i, j, N, a):
# Base case
if (i == len(a)) :
return 1;
# If answer for current state is already
# calculated then just return dp[i][j]
if (dp[i][j] != -1):
return dp[i][j];
# Answer initialized with zero
ans = 0;
# Tight condition true
if (j == 1) :
# Iterating from 0 to max value
# of tight condition
for k in range(0, int(a[i])+1):
# N is not allowed to use
if (k == N):
continue;
# When k is at max tight condition
# remains even in next state
if (k == int(a[i])):
# Calling recursive function
# for tight digit
ans += recur(i + 1, 1, N, a);
# Tight condition drops
else:
# Calling recursive function
# for digits less than tight
# condition digit
ans += recur(i + 1, 0, N, a);
# Tight condition false
else :
# Iterating for all digits
for k in range(0,10):
# Digit N is not possible
if (k == N):
continue;
# Calling recursive function for
# all digits from 0 to 9
ans += recur(i + 1, 0, N, a);
# Save and return dp value
dp[i][j]=ans;
return dp[i][j];
# Function to find numbers
# in the range L to R such that they
# do not contain digit N
def countInRange( N, A, B):
# Initializing dp array with - 1
for i in range(0,100001):
for j in range(0,2):
dp[i][j]=-1;
A -= 1;
L = str(A);
R = str(B);
# Numbers with sum of digits T from
# 1 to L - 1
ans1 = recur(0, 1, N, L);
# Initializing dp array with - 1
for i in range(0,100001):
for j in range(0,2):
dp[i][j]=-1;
# Numbers with sum of digits T in the
# range 1 to R
ans2 = recur(0, 1, N, R);
# Difference of ans2 and ans1
# will generate answer for required
# range
return ans2 - ans1;
# Driver Code
# Input 1
N = 5;
L = 1;
R = 10;
# Function Call
print(countInRange(N, L, R));
# Input 2
N1 = 5;
L1 = 1;
R1 = 100;
# Function Call
print(countInRange(N1, L1, R1));
# This code is contributed by agrawalpooja976.
C#
using System;
namespace GFG
{
static class Program
{
static readonly int MOD = 1_000_000_007;
// dp table initialized with -1
static int[,] dp = new int[100001, 2];
// Recursive Function to find numbers
// in the range L to R such that they
// do not contain digit N
static int Recur(int i, int j, int N, string a)
{
// Base case
if (i == a.Length)
{
return 1;
}
// If answer for current state is already
// calculated then just return dp[i][j]
if (dp[i, j] != -1)
return dp[i, j];
// Answer initialized with zero
int ans = 0;
// Tight condition true
if (j == 1)
{
// Iterating from 0 to max value
// of tight condition
for (int k = 0; k <= a[i] - '0'; k++)
{
// N is not allowed to use
if (k == N)
continue;
// When k is at max tight condition
// remains even in next state
if (k == a[i] - '0')
// Calling recursive function
// for tight digit
ans += Recur(i + 1, 1, N, a);
// Tight condition drops
else
ans += Recur(i + 1, 0, N, a);
}
}
// Tight condition false
else
{
// Iterating for all digits
for (int k = 0; k <= 9; k++)
{
// Digit N is not possible
if (k == N)
continue;
// Calling recursive function for
// all digits from 0 to 9
ans += Recur(i + 1, 0, N, a);
}
}
// Save and return dp value
return dp[i, j] = ans;
}
// Function to find numbers
// in the range L to R such that they
// do not contain digit N
static int CountInRange(int N, int A, int B)
{
// Initializing dp array with - 1
for (int i = 0; i < dp.GetLength(0); i++)
{
for (int j = 0; j < dp.GetLength(1); j++)
{
dp[i, j] = -1;
}
}
A--;
string L = A.ToString();
string R = B.ToString();
// Numbers with sum of digits T from
// 1 to L - 1
int ans1 = Recur(0, 1, N, L);
// Initializing dp array with - 1
for (int i = 0; i < dp.GetLength(0); i++)
{
for (int j = 0; j < dp.GetLength(1); j++)
{
dp[i, j] = -1;
}
}
// Numbers with sum of digits T in the
// range 1 to R
int ans2 = Recur(0, 1, N, R);
// Difference of ans2 and ans1
// will generate answer for required
// range
return ans2 - ans1;
}
// Main Method
static void Main(string[] args)
{
// Input 1
int N = 5;
int L = 1;
int R = 10;
// Function Call
Console.WriteLine(CountInRange(N, L, R));
// Input 2
int N1 = 5;
int L1 = 1;
int R1 = 100;
// Function Call
Console.WriteLine(CountInRange(N1, L1, R1));
}
}
}
// This code is contributed by surajrasr7277
JavaScript
// Javascript code to implement the approach
let MOD = 1e9 + 7;
// dp table initialized with -1
let dp = new Array(100001);
for(let i=0; i<100001; i++)
dp[i]= new Array(2);
// Recursive Function to find numbers
// in the range L to R such that they
// do not contain digit N
function recur(i, j, N, a)
{
// Base case
if (i == a.length) {
return 1;
}
// If answer for current state is already
// calculated then just return dp[i][j]
if (dp[i][j] != -1)
return dp[i][j];
// Answer initialized with zero
let ans = 0;
// Tight condition true
if (j == 1) {
// Iterating from 0 to max value
// of tight condition
for (let k = 0; k <= (parseInt(a[i])); k++) {
// N is not allowed to use
if (k == N)
continue;
// When k is at max tight condition
// remains even in next state
if (k == (parseInt(a[i])))
// Calling recursive function
// for tight digit
ans = ans + recur(i + 1, 1, N, a);
// Tight condition drops
else
// Calling recursive function
// for digits less than tight
// condition digit
ans = ans + recur(i + 1, 0, N, a);
}
}
// Tight condition false
else {
// Iterating for all digits
for (let k = 0; k <= 9; k++) {
// Digit N is not possible
if (k == N)
continue;
// Calling recursive function for
// all digits from 0 to 9
ans += recur(i + 1, 0, N, a);
}
}
// Save and return dp value
return dp[i][j] = ans;
}
// Function to find numbers
// in the range L to R such that they
// do not contain digit N
function countInRange(N, A, B)
{
// Initializing dp array with - 1
for(let i=0; i<100001; i++)
for(let j=0; j<2; j++)
dp[i][j]=-1;
A--;
let L = A.toString(), R = B.toString();
// Numbers with sum of digits T from
// 1 to L - 1
let ans1 = recur(0, 1, N, L);
// Initializing dp array with - 1
for(let i=0; i<100001; i++)
for(let j=0; j<2; j++)
dp[i][j]=-1;
// Numbers with sum of digits T in the
// range 1 to R
let ans2 = recur(0, 1, N, R);
// Difference of ans2 and ans1
// will generate answer for required
// range
return ans2 - ans1;
}
// Driver Code
// Input 1
let N = 5, L = 1, R = 10;
// Function Call
document.write(countInRange(N, L, R));
document.write("<br>");
// Input 2
let N1 = 5, L1 = 1, R1 = 100;
// Function Call
document.write(countInRange(N1, L1, R1));
Time Complexity: O(N), Where N is the number of digits to be filled
Auxiliary Space: O(N)
Related Articles:
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem