🔥✅🔥|| Simple & Straightforward solution || Median approach ||🔥✅🔥
Intuition\nThe optimal way would be finding a palindrome nearest to the median element\n\n# Approach\nThe approach is simple and straightforward, we find median
# Intuition\nThe optimal way would be finding a palindrome nearest to the median element\n\n# Approach\nThe approach is simple and straightforward, we find median then we check each element greater and lesser than it one by one. Using two pointers.\n\n# Please upvote & if any doubts, ask in comments.\n\n# Complexity\n- Time complexity: O(len(str(median)))\n\nBecause each palindrome is separated by an approximately distance of atmost ```10 ^ (Number of digits)```\n\n\n- Space complexity: O(1)\nNo extra space is used in this code\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n```C++ []\nclass Solution {\n    // Function to check if a number is palindrome\n    bool checkPalindrome(int x) {\n        string str = to_string(x);\n        return str == string(str.rbegin(), str.rend());\n    }\n\n    // Function to calculate the result\n    long long add(const vector<int>& nums, int target) {\n        long long result = 0;\n        for (int elem : nums) {\n            result += abs(elem - target);\n        }\n        return result;\n    }\npublic:\n    long long minimumCost(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int midIndex = nums.size() / 2;\n\n        // finding median\n        int mid;\n        if (nums.size() % 2 == 1) {\n            mid = nums[midIndex];\n            if (checkPalindrome(mid))\n                return add(nums, mid);\n        } else {\n            mid = (nums[midIndex] + nums[midIndex - 1]) / 2;\n            if (checkPalindrome(mid))\n                return add(nums, mid);\n        }\n\n        int p1 = mid + 1;\n        int p2 = mid - 1;\n\n        //  we are finding 2 palindrome, one less than other greater than median element\n        while (!(checkPalindrome(p1) && checkPalindrome(p2))) {\n            if (!checkPalindrome(p1))\n                p1++;\n            if (!checkPalindrome(p2))\n                p2--;\n        }\n\n        return min(add(nums, p1), add(nums, p2));\n    }\n};\n```\n```python []\nclass Solution:\n    def minimumCost(self, nums):\n        nums.sort()\n        midIndex = len(nums) // 2\n\n        def check_palindrome(x):\n            x = str(x)\n            return x == x[::-1]\n        \n        # function to calculate result\n        def add(nums, target):\n            result = 0\n            for elem in nums:\n                result += abs(elem - target)\n            return result\n\n        # finding median\n        if len(nums) % 2 == 1:\n            mid = nums[midIndex]\n            if check_palindrome(mid):\n                return add(nums, mid)\n        else:\n            mid = (nums[midIndex] + nums[midIndex - 1]) // 2\n            if check_palindrome(mid):\n                return add(nums, mid)\n\n        p_1 = mid + 1\n        p_2 = mid - 1\n\n        # we are finding 2 palindrome, one less than other greater than median element\n        while not (check_palindrome(p_1) and check_palindrome(p_2)):\n            if not check_palindrome(p_1):\n                p_1 += 1\n            if not check_palindrome(p_2):\n                p_2 -= 1\n\n        return min(add(nums, p_1), add(nums, p_2))\n```\n```Java []\nclass Solution {\n    // Function to check if a number is palindrome\n    private boolean checkPalindrome(int x) {\n        String str = Integer.toString(x);\n        return str.equals(new StringBuilder(str).reverse().toString());\n    }\n\n    // Function to calculate the result\n    private long add(int[] arr, int target) {\n        long result = 0;\n        for (int elem : arr) \n            result += Math.abs(elem - target);\n\n        return result;\n    }\n\n    public long minimumCost(int[] nums) {\n        Arrays.sort(nums);\n        int midIndex = nums.length / 2;\n\n        int mid;\n        if (nums.length % 2 == 1) {\n            mid = nums[midIndex];\n            if (checkPalindrome(mid))\n                return add(nums, mid);\n        } \n        else {\n            mid = (nums[midIndex] + nums[midIndex - 1]) / 2;\n            if (checkPalindrome(mid))\n                return add(nums, mid);\n        }\n\n        int p1 = mid + 1;\n        int p2 = mid - 1;\n\n        while (!(checkPalindrome(p1) && checkPalindrome(p2))) {\n            if (!checkPalindrome(p1))\n                p1++;\n            if (!checkPalindrome(p2))\n                p2--;\n        }\n        return Math.min(add(nums, p1), add(nums, p2));\n    }\n};\n```\n
['Two Pointers', 'String', 'String Matching', 'Python', 'C++', 'Java', 'Python3']