Easy Solution ✅✅ | Beats 100% ✔️✔️
Intuition\n Describe your first thoughts on how to solve this problem. \nIn sorted array middle element is the number which give lowest abs difference if you ca
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn sorted array middle element is the number which give lowest abs difference if you calculate.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nstep 1: sort the array.\nstep 2: store middle element in some variable.\nstep 3: create two new variable lower and higher.lower will tell any palindromic number <= middle and higher will tell any palindromic number >=middle\nstep 4: take minimum of abs(nums[i]-lower) and abs(nums[i]-higher).\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nT.C : O(nlogn)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nS.C : O(1)\n![7abc56.jpg](https://assets.leetcode.com/users/images/ddd79ee2-8f7c-45e6-9057-0e1d0322e6e1_1702972803.5630205.jpeg)\n\n# Code\n```\nclass Solution {\npublic:\n    //function to check if number is palindrome or not.\n    bool check(int num){\n        string s=to_string(num);\n        int n=s.size();\n        int low=0,high=n-1;\n        while(low<=high)\n        {\n            if(s[low]!=s[high])return false;\n            low++;\n            high--;\n        }\n        return true;\n    }\n    //function which calculates |nums[i] - x|\n    long long solve(vector<int>& nums,int x){\n        long long temp=0;\n        for(auto it : nums)temp+=abs(it-x);\n        return temp;\n    }\n    //function to implement\n    long long minimumCost(vector<int>& nums) {\n        long long ans=INT_MAX;\n        int n=nums.size();\n        sort(nums.begin(),nums.end());\n        //store middle element of array.\n        int mid=nums[n/2];\n        int lower=mid;\n        int higher=mid;\n        //find palindrome, exist in <=lower\n        while(!check(lower))lower--;\n        //find palindrome, exist in >= higher\n        while(!check(higher))higher++;\n        //we get two palindrome take that which give lower abs diff with each element.\n        ans=min(solve(nums,lower),solve(nums,higher));\n        return ans;\n    }\n};\n```
['C++']