<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:


    """
    we can try to construct a palindrome by considering the mirror
    for length = 2, we can use 1 character, then flip it to make a palindrome

    lower_bound = 

    for odd numbers, we consider the even + 1
    """
    def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]:
        digits = (intLength+1)//2 # len = 5, digit = 3, len = 6, digit = 3 
        lower_bound = 10 ** (digits-1)
        upper_bound = 10 ** digits - 1
        output = []
        for query in queries:
            left_digit = query - 1 + lower_bound
            if left_digit > upper_bound:
                output.append(-1)
            else:
                if intLength % 2 == 0: 
                    palindrome = int(str(left_digit) + str(left_digit)[::-1])
                else:
                    left_str = str(left_digit)[0:-1]
                    palindrome = int(str(left_digit) + left_str[::-1])
                output.append(palindrome)
        return output


    def kthPalindromeT(self, queries: List[int], intLength: int) -> List[int]:
        # for length K palindrome
        # if k = 1, there's 9 palindromes from 1 to 9
        # if k = 2, we can construct it as 11 to 22 to ... 99 which is 9 palindromes
        # k = 3, 101 111 121 ... 191 ... 202 , 212 ... 292 , 303 ... 909, ... , 999
        # middle number has 0 to 9 or 10 choices, then it's 1-9 or 9 choices for total of 90 choices
        # k = 4, 10 01, 11 11, 12 21 ... 99 99 from 99 to 10 = 89 choices
        # for each query, we can just construct the palindrome
        output = []
        for query in queries:
            a = max(query % 10 - 1, 0)
            b = query // 10
            if intLength % 2 == 0: # even length palindrome
                left_len = intLength // 2
                lower_bound = 10 ** (left_len - 1)
                upper_bound = 10 ** left_len - 1
                if query + lower_bound - 1 > upper_bound:
                    palindrome = str(-1)
                else:
                    left_int = lower_bound + query - 1
                    palindrome = str(left_int) + str(left_int)[::-1]
            else:
                left_len = (intLength-1) // 2 # remove middle core first
                lower_bound = 10 ** (left_len - 1) # if 1 digit, it's 1, 2 digit, it's 10, 3 digit, it's 100
                upper_bound = 10 ** left_len - 1

                left_int = lower_bound + b
                if left_int > upper_bound: # exceeds upper bound, can redirect to middle core number
                    left_int -= 1
                    a += (10-1)
                palindrome = str(left_int) + str(a) + str(left_int)[::-1]

            output.append(int(palindrome))
        return output
426