Understand the Absolute Value and Median
Intuition\n Describe your first thoughts on how to solve this problem. \nSort the array and find the median. The optimal palindrome is the closest palidromes on
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSort the array and find the median. The optimal palindrome is the closest palidromes on the left([0, median]) and right([median, 10^9]) side of the median.\n\nYou can find better approach to find the closet palindrome than mine dumb brutal force implementation.\n\n# Complexity\n- Time complexity: O(n logn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(maxlen(nums[i]))\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public static boolean isPalindrome(int number) {\n        String numStr = Integer.toString(number);\n        int left = 0;\n        int right = numStr.length() - 1;\n\n        while (left < right) {\n            if (numStr.charAt(left) != numStr.charAt(right)) {\n                return false;\n            }\n            left++;\n            right--;\n        }\n\n        return true;\n    }\n    public static long calcCost(int[] nums, int target) {\n        long ans = 0;\n        for (int num : nums) {\n            ans += Math.abs(num - target);\n        }\n        return ans;\n    }\n    public long minimumCost(int[] nums) {\n        int n = nums.length;\n        Arrays.sort(nums);\n        if ((n & 1) == 1) {\n            if (isPalindrome(nums[n / 2])) {\n                return calcCost(nums, nums[n / 2]);\n            }\n        }\n        long ans = -1;\n        int pali = -1;\n        for (int i = nums[n / 2]; ; i++) {\n            if (isPalindrome(i)) {\n                ans = calcCost(nums, i);\n                pali = i;\n                break;\n            }\n        }\n        for (int i = nums[n / 2] - 1; i >= 0; i--) {\n            if (isPalindrome(i)) {\n                ans = ans == -1 ? calcCost(nums, i) : Math.min(ans, calcCost(nums, i));\n                break;\n            }\n        }\n//         if (ans == -1 || ) {\n//             for (int i = nums[n - 1] + 1; ; i++) {\n//                 if (isPalindrome(i)) {\n//                     ans = calcCost(nums, i);\n//                     break;\n//                 }\n//             }\n//         }\n        \n//         if (ans == -1) {\n//             for (int i = nums[0] - 1; i > 0; i++) {\n//                 if (isPalindrome(i)) {\n//                     ans = calcCost(nums, i);\n//                     break;\n//                 }\n//             }\n//         }\n        return ans;\n    }\n}\n```
['Java']