<p>Given an integer array <code>queries</code> and a <strong>positive</strong> integer <code>intLength</code>, return <em>an array</em> <code>answer</code> <em>where</em> <code>answer[i]</code> <em>is either the </em><code>queries[i]<sup>th</sup></code> <em>smallest <strong>positive palindrome</strong> of length</em> <code>intLength</code> <em>or</em> <code>-1</code><em> if no such palindrome exists</em>.</p>

<p>A <strong>palindrome</strong> is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> queries = [1,2,3,4,5,90], intLength = 3
<strong>Output:</strong> [101,111,121,131,141,999]
<strong>Explanation:</strong>
The first few palindromes of length 3 are:
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...
The 90<sup>th</sup> palindrome of length 3 is 999.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> queries = [2,4,6], intLength = 4
<strong>Output:</strong> [1111,1331,1551]
<strong>Explanation:</strong>
The first six palindromes of length 4 are:
1001, 1111, 1221, 1331, 1441, and 1551.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= queries.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= queries[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= intLength&nbsp;&lt;= 15</code></li>
</ul>

class Solution:
    def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]:
        def get_kth_palindrome(k:int, int_len: int) -> int:
            max_k = 9 * 10**(math.ceil(int_len/2) - 1)
            if k > max_k:
                return -1
            # except central for odd
            half_digits_count = int(int_len/2)
            
            # left side, formula-based
            res = 10**(int_len//2 - (0 if int_len % 2 else 1)) + k - 1
            num = res
            if int_len % 2 == 1:
                num = res // 10
            # full
            for i in range(half_digits_count):
                res = res*10 + num % 10
                num = num // 10
            
            return res

            # num = k - 1
            # p_right, p_left, mult = 0, 0, 1
            
            # half_digits_count = math.ceil(int_len/2)
            # for i in range(half_digits_count):
            #     num, rem = divmod(num, 10)
            #     if i < half_digits_count - 1:
            #         digit = rem
            #     else:
            #         digit = rem + 1
            #     p_left = mult * digit + p_left
            #     p_right = p_right * 10 + digit
            #     mult *= 10
            
            # if int_len > 1 and int_len % 2 == 1:
            #     p_left = p_left // 10
            
            # if int_len > 1:
            #     res = p_left * mult + p_right
            # else:
            #     res = p_left
            # return res

        
        result = [0]*len(queries)
        for i in range(len(queries)):
            result[i] = get_kth_palindrome(queries[i], intLength)
        
        return result
512