<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 func(long firsthalf, bool even) {  // half of panlindrome
        long resultNum = firsthalf;  // 12 
        if (even == false) {  // n was of odd len
            firsthalf = firsthalf / 10;  // 12/ 10 = 1
        }

        while (firsthalf > 0) {  // 1 > 0
            int digit = firsthalf % 10;  // d = 1%10 = 1
            resultNum = (resultNum * 10) + digit; // (12 * 10) + 1 = 120+1 = 121
            firsthalf /= 10;  // 1/ 10 = 1
        }
        return resultNum;  // 1221
    }

    // 12345 123/ 10 = 12    12/10 = 1    12%10 = 2    12*10 + 2 = 122     1%10 = 1    122*10 + 1 = 1221
    string nearestPalindromic(string n) {  // "123"
        int len = n.length();

        int mid = len/2; // take half of string

        int firsthalfLength = (len%2 ==0) ? mid : (mid + 1); // agr even h toh mid aayega odd aaya toh mid + 1 hoga
        
        long firsthalf = stol(n.substr( 0, firsthalfLength));  // 123 ka substr(0, 2 )  // string to long int(s_to_l)
        
        vector<long> possibleAns;

        possibleAns.push_back(func(firsthalf, len%2 == 0));
        possibleAns.push_back(func(firsthalf+ 1, len%2 == 0));  // ye odd ke liye h 129 // substring 12 me +1 kr diye 13 hogya
        possibleAns.push_back(func(firsthalf- 1, len%2 == 0));  /// substring 12 me - 1 kr diye 11  // ab 1 ko centre bna diye 
        possibleAns.push_back((long)pow ( 10, len-1) - 1);  // 10^(3-1) - 1 = 100 - 1 = 99
        possibleAns.push_back((long) pow ( 10, len) + 1);  // 10^2 + 1 = 100 + 1 = 101


        long diff = LONG_MAX;  // yeh INT_MAX hai
        long result = INT_MAX;
        long originalNum = stol(n);

        for( long num : possibleAns){  // possibleAns 121 , 101 
            if( num == originalNum)  continue;  // ab nhi lenge agr 999 == 999 ho jye

            if(abs(num - originalNum) < diff){    // 131 - 123 < 187987678
                diff = abs(num - originalNum);
                result = num;

            }

             else if(abs(num - originalNum) == diff){  // difference bada ho jaye
                     result = min( result, num);

             }
        }

        return to_string(result) ;

    }
};
8100