Simple Check From Median
Intuition\nWhat if we just have to make all array elements equal not equalindromic? Min cost will come from median in this case. Now if we have to make it equal
# Intuition\nWhat if we just have to make all array elements equal not equalindromic? Min cost will come from median in this case. Now if we have to make it equalindromic we can find palindrome near to median. So just check for fisrt palindromic number less than median and greater than equal to median. Generate nearest palindromic using brute force.\n\n\n# Code\n```\nclass Solution\n{\n    int isPalindrome(int n)\n    {\n        int num = n, rev = 0;\n        for (; n; n /= 10)\n            rev = (rev * 10) + (n % 10);\n        return rev == num;\n    }\n\n    long long lessThan(int x) {\n        for (; !isPalindrome(x); x--);\n        return x;\n    }\n\n    long long greaterThan(int x)\n    {\n        if (x == 1e9)\n            return -1;\n        for (; !isPalindrome(x); x++);\n        return x;\n    }\n\n    long long calcCost(vector<int> &v, int x)\n    {\n        long long ans = 0;\n        for (int a : v)\n            ans += abs(a - x);\n        return ans;\n    }\n\n    long long cost(vector<int> &v, int x)\n    {\n        long long a = lessThan(x), b = greaterThan(x);\n        return min(calcCost(v, a), b != -1 ? calcCost(v, b) : LLONG_MAX);\n    }\n\npublic:\n    long long minimumCost(vector<int> &v)\n    {\n        sort(v.begin(), v.end());\n        int n = v.size();\n        long long ans = cost(v, v[n / 2]);\n        return n % 2 == 0 ? min(ans, cost(v, v[(n / 2) - 1])) : ans;\n    }\n};\n```
['C++']