Last Updated: 27 Aug, 2025

Kth digit of Nth Palindrome

Moderate
Asked in company
MAQ Software

Problem statement

You are given two positive integers, 'N' and 'K'.

The sequence of positive palindromic numbers starts as 1, 2, 3, ..., 8, 9, 11, 22, 33, ..., 99, 101, 111, ... and so on.

Your task is to first find the N-th number in this sequence. Then, you must find the K-th digit of that number, reading from left to right.


Input Format:
A single line containing two space-separated integers, 'N' and 'K'.


Output Format:
Print a single digit (0-9) which is the K-th digit of the N-th palindrome.
If 'K' is greater than the number of digits in the N-th palindrome, print -1.


Note:
The indexing for both 'N' and 'K' is 1-based.
The 1st palindrome is 1, the 9th is 9, the 10th is 11.

Approaches

01 Approach

The expected time complexity is O(log N)