EASIEST C++ SORTING APPROACH WITH MEDIAN
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1.JUST FIND THE SMALLES
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.JUST FIND THE SMALLEST PALINDROME NUMBER GREATER THAN MEDIAN AND ALSO THE GREATEST PALINDROME NUMBER LESS THAN MEDIAN\n \n2.CALCULATE THE COST FOR BOTH THE NUMBERS \n\ni.e, First we make all the numbers equal to that number we found in step 1 , which ultimately makes the array palindromic.\nExample : if we have 10 12 13 14 15 \ni) We check which is the greatest palindrome smaller than median(13)\n13 --> 12 -->11 \nso its 11(palindrome)\n\nii)We check which is the smallest palindrome greater than median(13)\n13 --> 14 -->15 ...... 21-->22\nso its 22(palindrome)\n\niii)The reason why 11 or 22 works because if we make all nos of array as either 11 or 22 we get ,\n11 11 11 11 11\n     (or)\n22 22 22 22 22\n\n3.we find the minimum cost between both the numbers(11,22) and return the min one\ni) the cost for 11 is 11\nii) the cost for 22 is 46 \nso we return 11\n\nOVERALL  ITS   A  BASIC  MATH  LOGIC\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n.logn) for sorting and finding median\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n# Code\n```\nclass Solution {\npublic:\n   bool is_palindrome(long long  n){\n       //check if the number is palindrome\n        long long temp = n;\n        long long ans = 0;\n        while(n>=1){\n            int dig = n%10;\n            ans = ans*10 + dig;\n            n/=10;\n        }\n        if(temp == ans) return true;\n        else return false;\n    }\n    long long calculate(vector<int>nums,int n){\n        //calculate cost for given value of n\n        long long cost =0;\n         for(auto x:nums){\n             cost+=(abs(x-n));\n\n         }\n         return cost;\n    }\n    long long greater(long long n){\n         //smallest palindrome number greater than median\n        while(!is_palindrome(n)){\n            n++;\n        }\n        return n;\n    }\n    long long smaller(long long n){\n        //largest palindrome number smaller than median\n          while(!is_palindrome(n)){\n            n--;\n        }\n        return n;\n    }\n    long long minimumCost(vector<int>& nums) {\n        int n = nums.size();\n        if(n==1) return 0;\n        sort(nums.begin(),nums.end());\n        int mid ;\n        if(n&1){\n            //median for odd size\n            mid = nums[n/2];\n        }\n        else {\n            //median for even size\n            mid = (nums[n/2]+nums[(n+1)/2])/2;\n        }\n        long long mini = INT_MAX;\n        // find first greatest palindrome number than median(mid)\n        long long great = greater(mid);\n        // find first smallest palindrome number than median(mid)\n        long long small = smaller(mid);\n        // calculate cost for both smaller and greater and find the smaller one \n        // among them and return it\n        long long res1 = calculate(nums,great);\n        long long res2 = calculate(nums,small);\n        return res1<res2?res1:res2;\n       \n    }\n};\n```
['Array', 'Math', 'Sorting', 'C++']