C++ || Beats 100 % users in TC || Easiest Code || Easy-to-Understand
Intuition\n Describe your first thoughts on how to solve this problem. \nFinding the nearest palindromic numbers to the median of the sorted array.\n\n# Approac
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFinding the nearest palindromic numbers to the median of the sorted array.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMake a function to check for palindrome number.\n\n1) Sort the given vector.\n2) Find the Median Value.\n3) Now do the increment as well as decrement in the median value untill you reach a palindromic value (Now you\'ll have two palindromic values ie incMid and decMid)\n4) Find the sum of costs for both the palindromic values (costInc and costDec)\n5) Return the minimum cost out of both the costs.\n\n# Complexity\n- Time complexity: O ( nlogn )  Due to the sorting\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Space complexity: O ( 1 ) \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool isPalindromic(long long x) {\n    long long original = x;\n    long long reversed = 0;\n\n    while (x > 0) {\n        long long digit = x % 10;\n        reversed = reversed * 10 + digit;\n        x /= 10;\n    }\n\n        return original == reversed;\n    \n    }\n    \n    long long minimumCost(vector<int>& nums) {\n       \n        long long n = nums.size();\n        sort(nums.begin(), nums.end());\n        long long mid = nums[n / 2]; \n        long long incMid = mid;\n        long long decMid = mid;\n        // while (!isPalindromic(incMid) && !isPalindromic(decMid)) {\n        //     incMid++; \n        //     decMid--; \n        // }\n         while (!isPalindromic(incMid)) {\n            incMid++;\n        }\n        while(!isPalindromic(decMid)){\n            decMid--;\n        }\n        long long costInc = 0, costDec = 0;\n        for (long long i = 0; i < n; ++i) {\n            costInc += abs(nums[i] - incMid);\n            costDec += abs(nums[i] - decMid);\n        }\n        return min(costInc, costDec);\n    }\n};\n```
['Sorting', 'C++']