Beginner friendly Python code, beats 99.5 %
Intuition\n After sorting the array, since we have to bring all numbers at one point, considering the elements at ends, cost will be minimum if there exist a pa
# Intuition\n<!-- After sorting the array, since we have to bring all numbers at one point, considering the elements at ends, cost will be minimum if there exist a palindrome in between these two numbers.-->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- O(max(nlog(n), max(nums)-min(nums))) -->\n\n- Space complexity:\n<!-- O(1) -->\n\n# Code\n```\nclass Solution:\n    def minimumCost(self, nums: List[int]) -> int:\n        n=len(nums)\n        nums.sort()\n        def cost(n):\n            ans=0\n            for i in nums:\n                ans+=abs(i-n)\n            return ans\n        def nearest_palindrome1(a):\n            while str(a)!=str(a)[::-1]:\n                a+=1\n            return a\n        def nearest_palindrome2(a):\n            while str(a)!=str(a)[::-1]:\n                a-=1\n            return a\n\n        if n%2==1:\n            m=nums[(n//2)]\n            if str(m) == str(m)[::-1]:\n                return cost(m)\n            else:\n                return min(cost(nearest_palindrome1(m)), cost(nearest_palindrome2(m)))\n        else:\n            m=nums[n//2]\n            p=nums[(n//2)-1]\n            if str(m)==str(m)[::-1]:\n                return cost(m)\n            elif str(p)==str(p)[::-1]:\n                return cost(p)\n            else:\n                return min(cost(nearest_palindrome1(m)), cost(nearest_palindrome2(m)))\n        \n\n```
['Python3']