Easy C++ Solution ||  100% beats || Basic Intuition🔥🔥
Intuition\n Describe your first thoughts on how to solve this problem. \n- As we see the description, there is no significance of indexing.\n- Hence, Sorting wi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- As we see the description, there is no significance of indexing.\n- Hence, Sorting will make things easier. \n- But sorting takes O(nlogn) time. \n- Hence we will shift all the smaller elements than median to the left of median and all the larger elements to the right. This will take O(n) time.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- We will find the two nearest palindromic numbers for the median element. (one smaller and one bigger)\n- We will consider this palindromic  number as \'x\' and find our ans for each.\n- At last, we will return the most optimal/minimum answer.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nFunctions:\n- isPalindromic :- O(number of digits)\n- closestPalindromicSmall :- O(1)\n- closestPalindromicLarge :- O(1)\n- calculateCost :- O(n), where n= size of nums array\n\nHence net  time complexity is although Linear i.e. O(n)\n\n- Space complexity: O(1)\n!\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```\nstatic int io_opt = []() {\n  ios::sync_with_stdio(false);\n  return 0;\n}();\nclass Solution {\npublic:\n    bool isPalindromic(int num) {\n        if (num < 0) return false;\n        int reversed = 0, original = num;\n        while (num != 0) {\n            reversed = reversed * 10 + num % 10;\n            num /= 10;\n        }\n        return original == reversed;\n    }\n    long long calculateCost(const vector<int>& nums, int target) {\n        long long cost = 0;\n        for (int num : nums) {\n            cost += abs(num - target);\n        }\n        return cost;\n    }\n    int closestPalindromicSmall(int num) {\n        if (isPalindromic(num)) {\n            return num;\n        }\n\n        int smaller = num - 1;\n        while (true) {\n            if (isPalindromic(smaller)) {\n                return smaller;\n            }\n            smaller--;\n        }\n    }\n    int closestPalindromicLarge(int num) {\n            if (isPalindromic(num)) {\n                return num;\n            }\n\n            int larger = num + 1;\n            while (true) {\n                if (isPalindromic(larger)) {\n                    return larger;\n                }\n                larger++;\n            }\n    }\n    long long minimumCost(vector<int>& nums) {\n        \n        long long minCost = LLONG_MAX;\n        nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end());\n        int medium = nums[nums.size() / 2];\n\n        minCost = min(calculateCost(nums, closestPalindromicSmall(medium)), minCost);\n        minCost = min(calculateCost(nums, closestPalindromicLarge(medium)), minCost);\n\n        return minCost;\n    }\n};\n```\n\n![Screenshot 2023-12-22 225011.png](https://assets.leetcode.com/users/images/17613cdc-e0a2-4926-bf6d-330b02835806_1703266467.582188.png)\n
['C++']