<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:
    string nearestPalindromic(string s) {
        int n = s.size();
        long half = stol(s.substr(0,(n+1)/2));

        vector<long> cans;
        cans.push_back(helper(half, n%2==0));
        cans.push_back(helper(half+1, n%2==0));
        cans.push_back(helper(half-1, n%2==0));
        cans.push_back((long)pow(10,n-1)-1);
        cans.push_back((long)pow(10,n)+1);

        long diff=LONG_MAX, ans=0, num = stol(s);

        for(auto can : cans){
            if (can == num){
                continue;
            }
            
            if (abs(can-num)<diff){
                ans = can;
                diff = abs(can-num);
            } else if (abs(can-num)==diff){
                ans = min(ans, can);
            }
        }

        return to_string(ans);
    }
private:
    long helper(long left, bool even){
        long res = left;
        if(!even){
            left /= 10;
        }

        while(left>0){
            res = res*10+left%10;
            left /= 10;
        }

        return res;
    }
};
8000