Best solution in Java
Intuition\nTo solve this problem, we can first identify a potential target palindromic number to make the array equalindromic. We can start by considering the m
**Intuition**\nTo solve this problem, we can first identify a potential target palindromic number to make the array equalindromic. We can start by considering the median element of the sorted array. If the array length is odd, the median element directly represents a palindromic number. If the array length is even, we can choose either the left or right median to represent a potential palindromic number.\n\n**Approach**\nImplement a helper function checkPalindrome to check if a number is palindromic. This function converts the number to a string and checks if it reads the same forwards and backwards.\nImplement a helper function calcCost to calculate the cost of making all elements in the array equal to a target number. This function calculates the sum of absolute differences between each element and the target number.\nSort the array nums to easily identify the median element(s).\nDetermine the potential target palindromic number:\nIf the array length is odd, consider the median element directly.\nIf the array length is even, consider both the left and right medians.\nFor each potential target palindromic number, calculate the cost of making all elements in the array equal to it.\nReturn the minimum cost among all potential target palindromic numbers.\n**Complexity**\n**Time complexity:** Sorting the array takes O(n log n) time. Checking if a number is palindromic takes O(log m) time, where m is the number of digits in the number. Calculating the cost for each potential target palindromic number takes O(n) time. Overall, the time complexity is dominated by sorting and is O(n log n).\n**Space complexity:** The space complexity is O(1) since we only use a constant amount of extra space for variables and function calls.\n\n# Code\nclass Solution {\n    public static boolean checkPalindrome(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 (checkPalindrome(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 (checkPalindrome(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 (checkPalindrome(i)) {\n                ans = ans == -1 ? calcCost(nums, i) : Math.min(ans, calcCost(nums, i));\n                break;\n            }\n        }\n\n        return ans;\n    }\n}\n\n
['Java']