Cpp solution || nearest palindrome!!
\nIntuition:-\nin these type of questions one thing you will find very common that it is best and optimal to change the array elements to the median element of 
\nIntuition:-\nin these type of questions one thing you will find very common that it is best and optimal to change the array elements to the median element of the array.In this question since all the array elements should be palindrome therefore we will find the nearest palindrome of the median element and change accordingly all the elements to that and calculate the cost.\n\nApproach:-\n1) sort the array and find the median element,check if the median element is itself a palindrome then calculate the ans.\n\n2)if,not calculate the nearest palindrome by first checking nearest palindrome greater than median element and element smaller than median element.\n\n3) Calculate the sum in both the cases and return the min of that as answer.\n\n# Code\n```\nclass Solution {\npublic:\n    bool check(long long int p) // function to check palindrome\n    {\n        string h = to_string(p);\n        int i = 0;\n        int j = h.length()-1;\n        while(i<=j)\n        {\n            if(h[i]!=h[j]) return false;\n            i++;\n            j--;\n        }\n        return true;\n    }\n    long long int checka(long long int k)//function to check nearest palindrome gretaer than median element\n    {\n        long long i = k+1;\n        while(true)\n        {\n            if(check(i))\n            {\n                break;\n            }\n            i++;\n        }\n        return i;\n    }\n    long long int checkp(long long int k)//function to check nearest palindrome smaller than median element\n    {\n        long long i = k-1;\n        while(i>0)\n        {\n            if(check(i))\n            {\n                break;\n            }\n            i--;\n        }\n        if(i<0) return -1;\n        return i;\n    }\n    long long gans(vector<int> &nums,long long j,int n)//calculating the cost.\n    {\n        long long sum = 0;\n        for(int i=0;i<n;i++)\n        {\n            long long y = nums[i];\n            sum = sum + abs(y-j);\n        }\n        return sum;\n    }\n    long long minimumCost(vector<int>& nums) {\n        int n = nums.size();\n        sort(nums.begin(),nums.end());\n        int mid = (n-1)/2;\n        long long int mide = nums[mid];\n        long long m1=INT_MAX,m2=INT_MAX;\n        if(check(mide))\n        {\n           return gans(nums,mide,n);\n        }\n        else\n        {\n            long long aage = checka(mide);\n            long long piche = checkp(mide);\n            if(piche!=-1)\n            {\n                m1 = gans(nums,aage,n);\n                m2 = gans(nums,piche,n);\n            }\n            else\n            {\n                m1 = gans(nums,aage,n);\n            }\n        }\n        return min(m1,m2);\n    }\n};\n```
['C++']