Number of digits in the nth number made of given four digits
Last Updated :
28 Apr, 2025
Given an integer n, find the number of digits in the n-th number formed using only the digits 1, 4, 6, and 9, when the numbers are arranged in ascending order.
1, 4, 6, 9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119, ....
Examples:
Input: n = 6
Output: 2
Explanation: The first 6 numbers in the given sequence are 1, 4, 6, 9, 11, and 14. The number of digits in the 6th number i.e. 14 is 2.
Input: n = 21
Output: 3
Explanation: The 21st digit of the series is 111 which has 3 digits.
[Naive Approach] - O(d * 4d) Time and O(1) Space
The idea is to generate numbers sequentially, starting from 1, and check if each number consists only of the digits 1, 4, 6, or 9. For every valid number found, a counter is incremented. This process continues until the counter reaches n. Once the n-th valid number is identified, the number of digits in that number is determined by repeatedly dividing it by 10 and counting the divisions.
C++
#include <bits/stdc++.h>
using namespace std;
// function to find the number of digits
// in the n-th number of the given sequence
int countDigit(int n) {
// to store count of number
// of given sequence
int count = 0;
// to store the current number
int num = 0;
// loop until count < n
while(count < n) {
// increment the number
num++;
// check if num is made of
// 1, 4, 6, or 9 only
vector<int> digits(10, 0);
int temp = num;
// find digits of current number
while(temp > 0) {
digits[temp % 10]++;
temp /= 10;
}
// true if number if made of 1, 4, 6, or 9 only
bool isValid = true;
// check if the number is made of 1, 4, 6, or 9 only
for(int i = 0; i <= 9; i++) {
if(i != 1 && i != 4 && i != 6 && i != 9) {
if(digits[i] > 0) {
isValid = false;
break;
}
}
}
if(isValid) {
count++;
}
}
int res = 0;
while(num > 0) {
res++;
num /= 10;
}
return res;
}
int main() {
int n = 21;
cout << countDigit(n);
return 0;
}
Java
import java.util.*;
public class GfG {
// function to find the number of digits
// in the n-th number of the given sequence
public static int countDigit(int n) {
// to store count of number
// of given sequence
int count = 0;
// to store the current number
int num = 0;
// loop until count < n
while(count < n) {
// increment the number
num++;
// check if num is made of
// 1, 4, 6, or 9 only
int[] digits = new int[10];
int temp = num;
// find digits of current number
while(temp > 0) {
digits[temp % 10]++;
temp /= 10;
}
// true if number if made of 1, 4, 6, or 9 only
boolean isValid = true;
// check if the number is made of 1, 4, 6, or 9 only
for(int i = 0; i <= 9; i++) {
if(i != 1 && i != 4 && i != 6 && i != 9) {
if(digits[i] > 0) {
isValid = false;
break;
}
}
}
if(isValid) {
count++;
}
}
int res = 0;
while(num > 0) {
res++;
num /= 10;
}
return res;
}
public static void main(String[] args) {
int n = 21;
System.out.print(countDigit(n));
}
}
Python
def countDigit(n):
# to store count of number
# of given sequence
count = 0
# to store the current number
num = 0
# loop until count < n
while count < n:
# increment the number
num += 1
# check if num is made of
# 1, 4, 6, or 9 only
digits = [0] * 10
temp = num
# find digits of current number
while temp > 0:
digits[temp % 10] += 1
temp //= 10
# true if number if made of 1, 4, 6, or 9 only
isValid = True
# check if the number is made of 1, 4, 6, or 9 only
for i in range(10):
if i not in (1, 4, 6, 9):
if digits[i] > 0:
isValid = False
break
if isValid:
count += 1
res = 0
while num > 0:
res += 1
num //= 10
return res
if __name__ == "__main__":
n = 21
print(countDigit(n))
C#
using System;
public class GfG {
// function to find the number of digits
// in the n-th number of the given sequence
public static int countDigit(int n) {
// to store count of number
// of given sequence
int count = 0;
// to store the current number
int num = 0;
// loop until count < n
while(count < n) {
// increment the number
num++;
// check if num is made of
// 1, 4, 6, or 9 only
int[] digits = new int[10];
int temp = num;
// find digits of current number
while(temp > 0) {
digits[temp % 10]++;
temp /= 10;
}
// true if number if made of 1, 4, 6, or 9 only
bool isValid = true;
// check if the number is made of 1, 4, 6, or 9 only
for(int i = 0; i <= 9; i++) {
if(i != 1 && i != 4 && i != 6 && i != 9) {
if(digits[i] > 0) {
isValid = false;
break;
}
}
}
if(isValid) {
count++;
}
}
int res = 0;
while(num > 0) {
res++;
num /= 10;
}
return res;
}
public static void Main(string[] args) {
int n = 21;
Console.Write(countDigit(n));
}
}
JavaScript
function countDigit(n) {
// to store count of number
// of given sequence
let count = 0;
// to store the current number
let num = 0;
// loop until count < n
while(count < n) {
// increment the number
num++;
// check if num is made of
// 1, 4, 6, or 9 only
const digits = Array(10).fill(0);
let temp = num;
// find digits of current number
while(temp > 0) {
digits[temp % 10]++;
temp = Math.floor(temp / 10);
}
// true if number if made of 1, 4, 6, or 9 only
let isValid = true;
// check if the number is made of 1, 4, 6, or 9 only
for(let i = 0; i <= 9; i++) {
if(i !== 1 && i !== 4 && i !== 6 && i !== 9) {
if(digits[i] > 0) {
isValid = false;
break;
}
}
}
if(isValid) {
count++;
}
}
let res = 0;
while(num > 0) {
res++;
num = Math.floor(num / 10);
}
return res;
}
const n = 21;
console.log(countDigit(n));
Time Complexity: O(d * 4^d), where d is the number of digits in the nth number and we need to repeat this process for each possible combination of digits, which is 4^d.
Space Complexity: O(1), where d is the number of digits in the nth number.
[Expected Approach] - O(log4 n) Time and O(1) Space
The idea is to exploit the fact that there are exactly 4^k numbers of length k (since each digit can be one of four choices). Instead of generating each number, we can compute in constant time how many k-digit numbers exist, keep a running total, and stop once we’ve reached or passed the n-th number. The current digit length at that point is the answer.
Follow the below given steps:
- Initialize
digit
to 1 - Initialize
num
to 4, representing the number of 1-digit numbers. - Initialize
count
to 4, the total numbers counted so far. - While
count < n
:- Multiply
num
by 4 to get the count of (digit+1)-digit numbers - Increment
digit
by 1 - Add
num
to count
- Return
digit
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
// function to find the number of digits
// in the n-th number of the given sequence
int countDigit(int n) {
// to store the number of digits
int digit = 1;
// to store count of numbers
// with digit digits, there
// are 4 such number of 1 digit
int num = 4;
// to store count of number
// of given sequence
int count = 4;
while(count < n) {
// multiply the number with 4
// to get the number of digits
// with digit+1 digits
num *= 4;
// increment the digit
digit++;
// add the count of numbers
// with digit+1 digits
count += num;
}
return digit;
}
int main() {
int n = 21;
cout << countDigit(n);
return 0;
}
Java
import java.util.*;
public class GfG {
// function to find the number of digits
// in the n-th number of the given sequence
public static int countDigit(int n) {
// to store the number of digits
int digit = 1;
// to store count of numbers
// with digit digits, there
// are 4 such number of 1 digit
int num = 4;
// to store count of number
// of given sequence
int count = 4;
while(count < n) {
// multiply the number with 4
// to get the number of digits
// with digit+1 digits
num *= 4;
// increment the digit
digit++;
// add the count of numbers
// with digit+1 digits
count += num;
}
return digit;
}
public static void main(String[] args) {
int n = 21;
System.out.print(countDigit(n));
}
}
Python
def countDigit(n):
# function to find the number of digits
# in the n-th number of the given sequence
# to store the number of digits
digit = 1
# to store count of numbers
# with digit digits, there
# are 4 such number of 1 digit
num = 4
# to store count of number
# of given sequence
count = 4
while count < n:
# multiply the number with 4
# to get the number of digits
# with digit+1 digits
num *= 4
# increment the digit
digit += 1
# add the count of numbers
# with digit+1 digits
count += num
return digit
if __name__ == "__main__":
n = 21
print(countDigit(n))
C#
using System;
public class GfG {
// function to find the number of digits
// in the n-th number of the given sequence
public static int countDigit(int n) {
// to store the number of digits
int digit = 1;
// to store count of numbers
// with digit digits, there
// are 4 such number of 1 digit
int num = 4;
// to store count of number
// of given sequence
int count = 4;
while(count < n) {
// multiply the number with 4
// to get the number of digits
// with digit+1 digits
num *= 4;
// increment the digit
digit++;
// add the count of numbers
// with digit+1 digits
count += num;
}
return digit;
}
public static void Main(string[] args) {
int n = 21;
Console.Write(countDigit(n));
}
}
JavaScript
function countDigit(n) {
// function to find the number of digits
// in the n-th number of the given sequence
// to store the number of digits
let digit = 1;
// to store count of numbers
// with digit digits, there
// are 4 such number of 1 digit
let num = 4;
// to store count of number
// of given sequence
let count = 4;
while(count < n) {
// multiply the number with 4
// to get the number of digits
// with digit+1 digits
num *= 4;
// increment the digit
digit++;
// add the count of numbers
// with digit+1 digits
count += num;
}
return digit;
}
const n = 21;
console.log(countDigit(n));
Similar Reads
Find Next number having distinct digits from the given number N Given a natural number N, the task is to find the next number having distinct digits from the given number.Examples: Input: N = 19 Output: 20 Explanation: Next number to 19 whose digits are different from 19 is 20.Input: N = 2019 Output: 3333 Explanation: Next number to 2019 whose digits are differe
10 min read
Find the n-th number made of even digits only Given a number n, find out the n-th positive number made up of even digits (0, 2, 4, 6, 8) only. First few numbers made of even digits are 0, 2, 4, 6, 8, 20, 22, 24....... Examples : Input : 2 Output : 2 Second number made of 0, 2, 4, 6, 8 is 2 Input : 10 Output : 28 Naive Approach A naive approach
14 min read
Get the kth smallest number using the digits of the given number Given a non-negative number n and a value k. Find the kth smallest number that can be formed using the digits of the given number n. It is guaranteed that the kth smallest number can be formed. Note that the number could be very large and may not even fit into long long int. Examples: Input : n = 12
11 min read
Find maximum number that can be formed using digits of a given number Given a number, write a program to find a maximum number that can be formed using all of the digits of this number.Examples: Input : 38293367Output : 98763332Input : 1203465Output: 6543210Simple Approach: The simple method to solve this problem is to extract and store the digits of the given number
6 min read
Number of n digit numbers that do not contain 9 Given a number n, find how many n digit number can be formed that does not contain 9 as it's digit.Examples: Input : 1 Output : 8 Explanation : Except 9, all numbers are possible Input : 2 Output : 72 Explanation : Except numbers from 90 - 99 and all two digit numbers that does not end with 9 are po
3 min read
Find position of the given number among the numbers made of 4 and 7 Consider a series of numbers composed of only digits 4 and 7. The first few numbers in the series are 4, 7, 44, 47, 74, 77, 444, .. etc. Given a number constructed by 4, 7 digits only, we need to find the position of this number in this series. Examples: Input : 7 Output : pos = 2 Input : 444 Output
5 min read