Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Nth Digit problem solution

YASH PAL, 31 July 2024

In this Leetcode Nth Digit problem solution you are given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …].

Leetcode Nth Digit problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

def findNthDigit(self, n: int) -> int:
    if n < 10:
        return n
    i = 0
    count = 0
    num = 1
    while True:
        next = ((9 * (i + 1)) * num)
        i += 1
        if count + next >= n:
            break
        num *= 10
        count += next
    count += 1
    move, digit = divmod(n - count, i)
    count += move * i
    num += move

    return int(str(num)[digit])

Problem solution in Java.

class Solution {
    public int findNthDigit(int n) {
        long step = 1;
        long size = 9 * (int)Math.pow(10, step-1);
        while (n > step * size) {
            n -= step * size;
            step++;
            size = 9 * (int)Math.pow(10, step-1);
        }        
        long number = size/9 + (long)Math.ceil((n*1.0d)/step) -1;
        char ansDigit = String.valueOf(number).charAt((int)((n%step-1+step)%step));
        return (int)(ansDigit - '0');
    }
}

Problem solution in C++.

class Solution {
public:
    #define ll long long
    ll power(int a){
        a--;
        ll i=1;
        while(a){
            a--;
            i*=10;
        }
        return i;
    }
    int findNthDigit(int n) {
        if(n<10)
            return n;
        ll div=9;
        ll c=1;
        while((n-div)>0){ 
            n-=div;
            c++;
            div=9*power(c)*c;
        }
        n--;
        string tmp=to_string(power(c)+n/c);
        cout << tmp;
        return (tmp[n%c]-'0');
    }
};

Problem solution in C.

int findNthDigit(int n){
        long digits_passed = 9;
        long digits_count = 1;
        long current_count = 0;
        long running_number = 0;
        long prev = 0;
        long diff, add, remainder;
        if(n <= 9)
                return n;
        while((current_count+ digits_passed*digits_count) < n){
                current_count += digits_passed*digits_count;
                digits_count++;
                digits_passed *= 10;
                running_number += (digits_passed/10);
        }
        diff = (n - current_count);
        if(diff % digits_count == 0){
                running_number += (diff/digits_count);
                return running_number%10;
        }
        else
        {
                prev = diff;
                diff += (digits_count - diff % digits_count);
                running_number += (diff/digits_count);
                prev = (prev % digits_count);
                digits_count = digits_count - prev;
                while(digits_count--)
                        running_number /= 10;
                return running_number%10;
        }
        return -1;

}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes