<p>Given a string <code>n</code> representing an integer, return <em>the closest integer (not including itself), which is a palindrome</em>. If there is a tie, return <em><strong>the smaller one</strong></em>.</p>

<p>The closest is defined as the absolute difference minimized between two integers.</p>

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

<pre>
<strong>Input:</strong> n = &quot;123&quot;
<strong>Output:</strong> &quot;121&quot;
</pre>

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

<pre>
<strong>Input:</strong> n = &quot;1&quot;
<strong>Output:</strong> &quot;0&quot;
<strong>Explanation:</strong> 0 and 2 are the closest palindromes but we return the smallest which is 0.
</pre>

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

<ul>
	<li><code>1 &lt;= n.length &lt;= 18</code></li>
	<li><code>n</code> consists of only digits.</li>
	<li><code>n</code> does not have leading zeros.</li>
	<li><code>n</code> is representing an integer in the range <code>[1, 10<sup>18</sup> - 1]</code>.</li>
</ul>

class Solution {
public:

long halfToPol(long num, bool odd )
{
    long temp = num;
    if(odd)
    {
        temp /=10;
    }
    while(temp)
    {
       
          
        num*=10;
        num += temp % 10;
        temp /=10;
      
    }
    return num;
}

string nearestPalindromic(string n) {
    size_t nsize = n.size();
    size_t mid = (nsize % 2 ==0) ? nsize/2 - 1: nsize/2;
    long firstHalf = stol(n.substr(0,mid+1));
    
    vector<long> maybePalindrome;
    maybePalindrome.push_back(halfToPol(firstHalf, nsize%2));
    maybePalindrome.push_back(halfToPol(firstHalf+1, nsize%2));
    maybePalindrome.push_back(halfToPol(firstHalf-1, nsize%2));
        maybePalindrome.push_back((long)pow(10,nsize-1)-1);
    maybePalindrome.push_back((long)pow(10,nsize)+1);
 //   maybePalindrome.push_back(
    long diff = LONG_MAX; long nl = stol(n); long res =0;
    for(auto& cand : maybePalindrome)
    {
           if(cand == nl)
            continue;
        long temp =abs(nl-cand);
        if(temp<diff)
        {
            diff =temp;
            res = cand;
        }
        else if(temp == diff)
        {
            res =min(cand,res);
        }
    }
    
    
    return to_string(res);
   }



};
8300