(Explained with proofs in Video Solution ✅) (Median)( why not Mean❓ and why Till 10^4 🙋‍♂️)
Intuition\n Describe your first thoughts on how to solve this problem. \nBuilding a palindromes to check ranges and proving why mean will fail\n\n# Easy Video E
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBuilding a palindromes to check ranges and proving why mean will fail\n\n# ***Easy Video Explanation***\n\nhttps://youtu.be/TQAc20MPW9M\n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nOur code defines a Solution class with functions to check if a number is a palindrome (isPal), calculate the cost of transforming an array to a target value (calculateCost), and find the minimum cost of transforming the array into a palindrome (minimumCost).\n\nThe minimumCost function first sorts the array and determines the middle value, considering odd and even lengths. It initializes the ans variable with a large value. If the middle value is a palindrome, it calculates the cost using the calculateCost function. Then, it explores the neighboring values to the left and right until it finds palindromes, updating left and right.\n\nThe final result is the minimum cost among the initial middle, left, and right values. The code utilizes long data types to handle large numbers and follows a similar logic in both Java and Python, adjusting syntax and data types accordingly.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N*log(N))\n    \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N)\n    \n\n# Code\n\n\n```C++ []\nclass Solution {\npublic:\n    bool isPal(int n) {\n        long long r, sum = 0, temp;\n        temp = n;\n        while (n > 0) {\n            r = n % 10;\n            sum = (sum * 10) + r;\n            n = n / 10;\n        }\n        return temp == sum;\n    }\n\n    long long calculateCost(vector<int>& nums, int r) {\n        long long cost = 0;\n        for (int n : nums) {\n            cost += abs(n - r);\n        }\n        return cost;\n    }\n    long long minimumCost(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        int middle = 0;\n        if (n == 1) return 0;\n\n        if (n % 2 != 0) {\n            middle = nums[(n) / 2];\n        } else {\n            middle = (nums[(n + 1) / 2] + nums[n / 2]) / 2;\n        }\n        long long ans=1e15;\n        if(isPal(middle)) {\n            ans=calculateCost(nums,middle);\n        }\n        \n        long long low=middle-1;\n        long long high=middle+1;\n        long long left=-1,right=-1;\n        while(left==-1 || right==-1) {\n            if(left==-1 && isPal(low)) {\n                left=low;\n            }\n            if(right==-1 && isPal(high)) {\n                right=high;\n            }\n            low--;\n            high++;\n        }\n\n        return min(ans,min(calculateCost(nums,right),calculateCost(nums,left)));\n    }\n};\n```\n```python []\nclass Solution:\n    def isPal(self, n):\n        temp = n\n        rev = 0\n        while n > 0:\n            rev = rev * 10 + n % 10\n            n = n // 10\n        return temp == rev\n\n    def calculateCost(self, nums, r):\n        cost = 0\n        for n in nums:\n            cost += abs(n - r)\n        return cost\n\n    def minimumCost(self, nums):\n        nums.sort()\n        n = len(nums)\n        middle = 0\n        if n == 1:\n            return 0\n\n        if n % 2 != 0:\n            middle = nums[n // 2]\n        else:\n            middle = (nums[(n + 1) // 2] + nums[n // 2]) // 2\n        ans = 1e15\n        if self.isPal(middle):\n            ans = self.calculateCost(nums, middle)\n\n        low, high = middle - 1, middle + 1\n        left, right = -1, -1\n        while left == -1 or right == -1:\n            if left == -1 and self.isPal(low):\n                left = low\n            if right == -1 and self.isPal(high):\n                right = high\n            low -= 1\n            high += 1\n\n        return min(ans, min(self.calculateCost(nums, right), self.calculateCost(nums, left)))\n\n```\n```Java []\n\nimport java.util.Arrays;\n\npublic class Solution {\n    public boolean isPal(int n) {\n        long r, sum = 0, temp;\n        temp = n;\n        while (n > 0) {\n            r = n % 10;\n            sum = (sum * 10) + r;\n            n = n / 10;\n        }\n        return temp == sum;\n    }\n\n    public long calculateCost(int[] nums, int r) {\n        long cost = 0;\n        for (int n : nums) {\n            cost += Math.abs(n - r);\n        }\n        return cost;\n    }\n\n    public long minimumCost(int[] nums) {\n        Arrays.sort(nums);\n        int n = nums.length;\n        int middle = 0;\n        if (n == 1) return 0;\n\n        if (n % 2 != 0) {\n            middle = nums[n / 2];\n        } else {\n            middle = (nums[(n + 1) / 2] + nums[n / 2]) / 2;\n        }\n        long ans = 1e15;\n        if (isPal(middle)) {\n            ans = calculateCost(nums, middle);\n        }\n\n        long low = middle - 1;\n        long high = middle + 1;\n        long left = -1, right = -1;\n        while (left == -1 || right == -1) {\n            if (left == -1 && isPal((int) low)) {\n                left = low;\n            }\n            if (right == -1 && isPal((int) high)) {\n                right = high;\n            }\n            low--;\n            high++;\n        }\n\n        return Math.min(ans, Math.min(calculateCost(nums, (int) right), calculateCost(nums, (int) left)));\n    }\n}\n\n```\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n***It Motivates me to record more videos***\n\n*Thank you* \uD83D\uDE00
['Math', 'Greedy', 'Python', 'C++', 'Java']