<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 {
public:
    vector<long long> kthPalindrome(vector<int>& queries, int intLength) {
        // for a given length
        // smallest palindromes:
        // [1 00 00 00 1]
        // [1 00 11 00 1]
        // [1 00 99 00 1]
        // [1 0999 90 1]
        // length ceil(n/2)
        // [1 xxxx]

        // Example for 5 digit plandrome
        // ciel(5/2) = 3
        // [100] -> 1
        // [199] -> 100
        // [1xxx] + (query - 1)
        // For each given length
        // Smallest palindromes given by following sequence:
        // set outer inds to 1
        // Start at middle or middle two indices and go 1-9, then use two next most inner, etc
        int digs_half = (intLength>>1)-1;
        if(intLength&1) {
            ++digs_half;
        }
        long long int start_num = pow(10, digs_half);
        long long int max_pal = start_num*9;
        --start_num;
        vector<long long int> res;
        for(int q : queries) {
            if(q > max_pal) {
                res.push_back(-1);
                continue;
            }
            long long int first_half = start_num + q;
            if(intLength == 1) {
                res.push_back(first_half);
                continue;
            }
            // 10804323632340801
            long long int first_half_cpy = first_half;
            if(intLength & 1) {
                first_half_cpy /= 10;
            }
            while(first_half_cpy) {
                first_half *= 10;
                first_half += first_half_cpy % 10;
                first_half_cpy /= 10;
            }
            res.push_back(first_half);
        }
        return res;
    }
};
58900