[Greedy] Get middle element and check adjacent palindromic numbers
\n\n# Code\n\nclass Solution {\npublic:\n    bool checkPalindrome(int val) {\n        string r = to_string(val);\n        string r1 = r;\n        reverse(r1.beg
\n\n# Code\n```\nclass Solution {\npublic:\n    bool checkPalindrome(int val) {\n        string r = to_string(val);\n        string r1 = r;\n        reverse(r1.begin(), r1.end());\n        if(r == r1) return true;\n        return false;\n    }\n    long long getCost(int val, vector<int>& nums) {\n        int n = nums.size();\n        long long ans = 0LL;\n        for(int i = 0; i < n; i++) {\n            ans = (long long)(ans + abs(nums[i] - val));\n        }\n        return ans;\n    }\n    long long getCostForTwoValues(int middle, vector<int> &nums) {\n        long long ans = LLONG_MAX;\n        int k = middle;\n        int k1 = middle;\n        while(checkPalindrome(k) == false) {\n            k--;\n        }\n        while(checkPalindrome(k1) == false) {\n            k1++;\n        }\n        long long op1 = getCost(k, nums);\n        long long op2 = getCost(k1, nums);\n        ans = min({ans, op1, op2});\n        return ans;\n    }\n    long long minimumCost(vector<int>& nums) {\n        int n = nums.size();\n        long long ans = LLONG_MAX;\n        sort(nums.begin(), nums.end());\n        if(n & 1) {\n            int middle = nums[n / 2];\n            if(checkPalindrome(middle)) {\n                ans = getCost(middle, nums);\n            } else {\n                ans = getCostForTwoValues(middle, nums);\n            }\n            return ans;\n        } else {\n            int middle1 = nums[n / 2];\n            int middle2 = nums[(n / 2) - 1];\n            if(checkPalindrome(middle1)) {\n                ans = min(ans, getCost(middle1, nums));\n            }\n            if(checkPalindrome(middle2)) {\n                ans = min(ans, getCost(middle2, nums));\n            }\n            ans = min({ans,getCostForTwoValues(middle1, nums), getCostForTwoValues(middle2, nums)});\n            return ans;\n        }\n    }\n};\n```
['C++']