<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 closest="";
    int minim=INT_MAX;
    void close(string a, string n)
    {
        long long aa=0;
        if(a.size()>0)
        {
            cout<<a<<endl;
            aa=stol(a);
        }
        else
            return;
        long na=stol(n);
        if(abs(aa-na)<=minim and a!=n)
        {
            if(abs(aa-na)==minim)
            {
                minim=abs(aa-na);
                cout<<closest<<" "<<a<<endl;
                closest=stol(closest)<aa?closest:a;
            }
            else
            {
                minim=abs(aa-na);
                closest=a;
            }
            
        }
    }
    string nearestPalindromic(string n) {
        

        
        bool even=n.size()%2==0;
        string fh= n.substr(0,(n.size()-1)/2 + 1);
        string n1=check(fh,even);
        close(n1,n);
        cout<<n1<<endl;


        string n2c=fh;
        long ln2c=stol(n2c)+1;
        string n2=check(to_string(ln2c),even);
        close(n2,n);
        cout<<n2<<endl;
        
        
        string n3c=fh;
        long ln3c=stol(n2c)-1;
        string n3=check(to_string(ln3c),even);
        close(n3,n);
        cout<<n3<<endl;
        
        string n4="";
        for(int i=0;i<n.size()-1;i++)
        {
             if(n.size()-1>n.size())
                break;
            n4=n4+'9';
        }
        string n7=n4+"9";
        cout<<n4<<endl;
        cout<<n7<<endl;
        cout<<"here"<<endl;
        close(n4,n);
        close(n7,n);
 
        string n5="1";
        for(int i=0;i<n.size()-2;i++)
        {
            if(n.size()-2>n.size())
                break;
            // cout<<i<<" "<<n.size()<<endl;
            // cout<<"runs"<<endl;
            n5=n5+'0';
        }
        string n6= n5+"01";
        n5=n5+"1";
        cout<<n5<<endl;
        cout<<n6<<endl;
        close(n5,n);
        close(n6,n);
        
        return closest;
    }
    string check(string a, bool even)
    {
        if(even)
        {
            string cp=a;
            reverse(a.begin(),a.end());
            return cp+a;
        }
        else
        {
            string cp=a;
            reverse(a.begin(),a.end());
            return cp.substr(0)+a.substr(1);
        }
    }
};
10800