<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:

    bool palindromize(string& x){
        bool isPalindrome = true;
        int n = x.size();
        int mid = n / 2;
        int toleft, toright;
        if(n & 1){
            toleft = mid - 1, toright = mid + 1;
        } else {
            toleft = mid - 1, toright = mid;
        }
        while(toleft >= 0 && toright < n){
            isPalindrome = isPalindrome && x[toright]==x[toleft];
            x[toright++] = x[toleft--];
        }
        return isPalindrome;
    }

    string nearestPalindromic(string& number) {

        string num = number;

        int n = number.size();

        // if(number == "11")
        //     return "9";

        if(n == 1)                             // 1,2,3,...,9
            return string(1, number[0] - 1);
        
        // if(number == "1" + string(n - 1, '0')) // 10,100,1000,...
        //     return string(n - 1, '9');

        // if(number == string(n, '9'))          // 99,999,9999,...
        //     return "1"+string(n-1,'0')+"1";

        // regular number (not palindrome)
        bool wasOrigniallyPalindrome = palindromize(number);

        vector<string> trials;
        trials.push_back(string(n - 1, '9'));
        trials.push_back("1"+string(n-1,'0')+"1");

        if(!wasOrigniallyPalindrome)
            trials.push_back(number);
        

       int mid = n / 2;
       if(n&1){
            if(1){
                long long tnum = strToll(number.substr(0,n/2 + 1));
                tnum -= 1;
                string temp = to_string(tnum) + reverseStr(to_string(tnum).substr(0, n/2));
                trials.push_back(temp);
            }

            if(1){
                long long tnum = strToll(number.substr(0,n/2 + 1));
                tnum += 1;
                string temp = to_string(tnum) + reverseStr(to_string(tnum).substr(0, n/2));
                trials.push_back(temp);
            }
       } else {

            if(1){
                long long tnum = strToll(number.substr(0,n / 2));
                tnum -= 1;
                string temp = to_string(tnum) + reverseStr(to_string(tnum));
                trials.push_back(temp);
            }

            if(true){
                long long tnum = strToll(number.substr(0,n / 2));
                tnum += 1;
                string temp = to_string(tnum) + reverseStr(to_string(tnum));
                trials.push_back(temp);
            }

       }
        // cout << trials[0] << endl << trials[1] << endl;
        string closest = getClosest(trials[0], trials[1], num);
        for(int i = 2; i < trials.size(); i++){
            // cout << trials[i] << endl;
            closest = getClosest(closest, trials[i], num);
        }

        return closest;
    }


    string reverseStr(string x){
        string y = x;
        reverse(y.begin(), y.end());
        return y;
    }

    long long strToll(string x){
        stringstream sstr(x);
        long long num;
        sstr >> num;
        return num;
    }

    string getClosest(string& num1, string& num2, string& number){
        
        if(abs(strToll(num1) - strToll(number)) == abs(strToll(num2)-strToll(number)))
            if(strToll(num1) < strToll(num2))
                return num1;
            else
                return num2;
        else if(abs(strToll(num1) - strToll(number)) < abs(strToll(num2)-strToll(number)))
            return num1;
        
        return num2;
    }
};
11000