<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 long stoi1(const std::string& str) {
        long long result = 0;
        int sign = 1;
        size_t i = 0;

        // Ignore leading whitespaces
        while (i < str.length() && isspace(str[i])) {
            i++;
        }

        // Handle optional sign
        if (i < str.length() && (str[i] == '-' || str[i] == '+')) {
            if (str[i] == '-') {
                sign = -1;
            }
            i++;
        }

        // Convert characters to integer
        while (i < str.length() && isdigit(str[i])) {
            int digit = str[i] - '0';
            
            // Check for overflow/underflow before multiplying by 10
            if (result > (LLONG_MAX - digit) / 10) {
                return (sign == 1) ? LLONG_MAX : LLONG_MIN;
            }

            result = result * 10 + digit;
            i++;
        }

        return sign * result;
    }
    string absoluteDifference(std::string str1, std::string str2) {
    // Ensure str1 is the larger number
    if (str1.length() < str2.length() || (str1.length() == str2.length() && str1 < str2)) {
        std::swap(str1, str2);
    }

    // Reverse strings to start subtraction from the least significant digit
    std::reverse(str1.begin(), str1.end());
    std::reverse(str2.begin(), str2.end());

    std::string result = "";
    int carry = 0;

    // Subtract corresponding digits of both strings
    for (size_t i = 0; i < str1.length(); ++i) {
        int digit1 = str1[i] - '0';
        int digit2 = (i < str2.length()) ? str2[i] - '0' : 0;

        int diff = digit1 - digit2 - carry;

        if (diff < 0) {
            diff += 10;
            carry = 1;
        } else {
            carry = 0;
        }

        result.push_back(diff + '0');
    }

    // Remove leading zeros
    while (result.length() > 1 && result.back() == '0') {
        result.pop_back();
    }

    // Reverse the result string to get the final answer
    std::reverse(result.begin(), result.end());

    return result;
}
    string nearestPalindromic(string n) {
        bool fl=(n.length()%2);
        string s=n.substr(0,(n.length()-1)/2+1);
        int x=stoi1(s);
        int y=x;

        x--;y++;

        string s1=to_string(x);
        string s2=to_string(y);

        string s11=s1;
        if(fl)
        s11.pop_back();
        reverse(s11.begin(),s11.end());
        string s22=s2;
        if(fl)
        s22.pop_back();
        reverse(s22.begin(),s22.end());

        string res1=absoluteDifference(s1+s11,n);
        string res2=absoluteDifference(s2+s22,n);

        string n1=n;
        for(int i=n.length()/2;i<n.length();i++)
        {
            n[i]=n[n.length()-i-1];
        }

        string res3=absoluteDifference(n,n1);
        string t1,t2;
        for(int i=1;i<n1.length();i++)
        t1.push_back('9');

        t2.push_back('1');
        for(int i=1;i<n1.length();i++)
        t2.push_back('0');
        t2.push_back('1');

        string res4=absoluteDifference(t1,n1);
        string res5=absoluteDifference(t2,n1);
        string resl=s2+s22;
        if(stoi1(absoluteDifference(resl,n1))>stoi1(res3))
        resl = n;
        if(stoi1(absoluteDifference(resl,n1))==stoi1(res3))
        if(stoi1(n)<stoi1(resl))
        resl=n;

        if(stoi1(absoluteDifference(resl,n1))>stoi1(res1))
        resl = s1+s11;
        if(stoi1(absoluteDifference(resl,n1))==stoi1(res1))
        if(stoi1(s1+s11)<stoi1(resl))
        resl = s1+s11;

        if(stoi1(absoluteDifference(resl,n1))>stoi1(res4))
        resl = t1;
        if(stoi1(absoluteDifference(resl,n1))==stoi1(res4))
        if(stoi1(t1)<stoi1(resl))
        resl = t1;

        if(stoi1(absoluteDifference(resl,n1))>stoi1(res5))
        resl=t2;
        if(stoi1(absoluteDifference(resl,n1))==stoi1(res5))
        if(stoi1(t2)<stoi1(resl))
        resl = t2;

        // cout<<s1+s11<<" "<<res1<<endl<<s2+s22<<" "<<res2<<endl<<n<<" "<<res3<<endl<<t1<<" "<<res4<<endl<<t2<<" "<<res5<<endl;

        if(resl==n1)
        {
            resl=s2+s22;
            if(stoi1(absoluteDifference(resl,n1))>stoi1(res1))
            resl = s1+s11;
            if(stoi1(absoluteDifference(resl,n1))==stoi1(res1))
            if(stoi1(s1+s11)<stoi1(resl))
            resl = s1+s11;

            if(stoi1(absoluteDifference(resl,n1))>stoi1(res4))
            resl = t1;
            if(stoi1(absoluteDifference(resl,n1))==stoi1(res4))
            if(stoi1(t1)<stoi1(resl))
            resl = t1;

            if(stoi1(absoluteDifference(resl,n1))>stoi1(res5))
            resl=t2;
            if(stoi1(absoluteDifference(resl,n1))==stoi1(res5))
            if(stoi1(t2)<stoi1(resl))
            resl = t2;
        }
        return resl;
    }
};
10700