Open In App

Number of digits in the nth number made of given four digits

Last Updated : 28 Apr, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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));

Output
3

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));

Output
3

Article Tags :
Practice Tags :

Similar Reads