medium
Intuition\n\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Tim
# Intuition\n![\u7B14\u8BB0 2023\u5E7412\u670821\u65E5.jpeg](https://assets.leetcode.com/users/images/f25cf3e0-d63b-4872-b503-31eff247a543_1703204315.2200801.jpeg)\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n\n    public static boolean isPalindrome(int num) {\n        String numStr = Integer.toString(num);\n        return numStr.equals(new StringBuilder(numStr).reverse().toString());\n    }\n    public static int findNearestSmallerPalindrome(int num) {\n        int smaller = num;\n\n        while (true) {\n            if (isPalindrome(smaller)) {\n                return smaller;\n            }\n\n            smaller--;\n        }\n    }\n    \n    public static int findNearestLargerPalindrome(int num) {\n        int larger = num;\n\n        while (true) {\n            if (isPalindrome(larger)) {\n                return larger;\n            }\n\n            larger++;\n        }\n    }\n    \n\n    \n\n    public long minimumCost(int[] nums) {\n        \n\n        Arrays.sort(nums);\n\n        int n = nums.length;\n        \n\n        int m = nums[n / 2];\n\n        int left = findNearestLargerPalindrome(m);\n \n        \n        long cost1 = 0;\n        \n        for (int num: nums) {\n            cost1 += Math.abs(num - left);\n        }\n        \n        \n        long cost2 = 0;\n        \n        long right = findNearestSmallerPalindrome(m);\n       \n        for (int num: nums) {\n            cost2 += Math.abs(num - right);\n        }\n\n\n        \n        return Math.min(cost1, cost2);\n        \n    }\n}\n```
['Java']