✅☑[C++/C/Java/Python/JavaScript] || Beats 100% || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n1. check(int t): This function checks whether an integer t is a palindrome.
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n*(Also explained in the code)*\n\n1. **check(int t):** This function checks whether an integer `t` is a palindrome. It first converts the integer to a string and then traverses the string characters from both ends towards the middle, checking if the characters match symmetrically. If they do, the function returns `true`; otherwise, it returns `false`.\n\n1. **minimumCost(vector<int>& nums):** This function aims to find the minimum cost required to make all elements in the `nums` vector equal. It does so by performing the following steps:\n\n    - Sorts the input array `nums`.\n    - Determines the middle elements `first` and `second` based on the array size. `second` is adjusted if the array length is even.\n    - Searches for the smallest palindrome value within specific ranges: `[first, second]`, `[first, 0]`, and `[second, INT_MAX]`.\n    - For each valid palindrome found in these ranges, it calculates the total cost required to make all elements equal to that palindrome value.\n    - Finally, it returns the minimum cost obtained among these possibilities.\n\n\n\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    // Function to check if a number is a palindrome\n    bool check(int t) {\n        // Convert the number to a string\n        string temp = to_string(t);\n        int i = 0, j = temp.size() - 1;\n\n        // Check for palindrome property\n        while (i < j) {\n            if (temp[i] != temp[j]) {\n                return false;\n            }\n            i++;\n            j--;\n        }\n        return true;\n    }\n    \n    // Function to find the minimum cost to make all elements in \'nums\' equal\n    long long minimumCost(vector<int>& nums) {\n        sort(nums.begin(), nums.end()); // Sort the input array\n        int n = nums.size();\n        int first = nums[n / 2]; // Middle element if the array length is odd\n        int second = nums[n / 2]; // Middle element if the array length is odd\n        long long ans = 1e18; // Set initial answer to a large value\n\n        // Adjust \'second\' if the array length is even\n        if (n % 2 == 0) {\n            second = nums[(n + 1) / 2];\n        }\n        \n        int temp = -1;\n\n        // Searching for the smallest palindrome value in the range [first, second]\n        for (int q = first; q <= second; ++q) {\n            if (check(q)) {\n                temp = q;\n                break;\n            }\n        }\n\n        // If a palindrome is found within the range, calculate the total cost\n        if (temp != -1) {\n            long long fo = 0;\n            for (int i = 0; i < n; ++i) {\n                fo += abs(nums[i] - temp);\n            }\n            ans = min(ans, fo);\n        }\n        \n        temp = -1;\n\n        // Searching for the smallest palindrome value in the range [first, 0]\n        for (int q = first; q >= 0; --q) {\n            if (check(q)) {\n                temp = q;\n                break;\n            }\n        }\n\n        // If a palindrome is found within the range, calculate the total cost\n        if (temp != -1) {\n            long long fo = 0;\n            for (int i = 0; i < n; ++i) {\n                fo += abs(nums[i] - temp);\n            }\n            ans = min(ans, fo);\n        }\n\n        temp = -1;\n\n        // Searching for the smallest palindrome value in the range [second, INT_MAX]\n        for (int q = second; q < INT_MAX; ++q) {\n            if (check(q)) {\n                temp = q;\n                break;\n            }\n        }\n\n        // If a palindrome is found within the range, calculate the total cost\n        if (temp != -1) {\n            long long fo = 0;\n            for (int i = 0; i < n; ++i) {\n                fo += abs(nums[i] - temp);\n            }\n            ans = min(ans, fo);\n        }\n        \n        // Return the minimum cost\n        return ans;\n    }\n};\n\n\n\n```\n```C []\n#include <stdio.h>\n#include <stdlib.h>\n#include <limits.h>\n\nint check(int t) {\n    char temp[20];\n    int i = 0, j;\n    sprintf(temp, "%d", t);\n    j = strlen(temp) - 1;\n    while (i < j) {\n        if (temp[i] != temp[j]) {\n            return 0;\n        }\n        i++;\n        j--;\n    }\n    return 1;\n}\n\nlong long minimumCost(int nums[], int length) {\n    qsort(nums, length, sizeof(int), compare);\n    int n = length;\n    int first = nums[n / 2];\n    int second = nums[n / 2];\n    long long ans = 1000000000000000000;\n\n    if (n % 2 == 0) {\n        second = nums[(n + 1) / 2];\n    }\n\n    int temp = -1;\n\n    for (int q = first; q <= second; ++q) {\n        if (check(q)) {\n            temp = q;\n            break;\n        }\n    }\n\n    if (temp != -1) {\n        long long fo = 0;\n        for (int i = 0; i < n; ++i) {\n            fo += abs(nums[i] - temp);\n        }\n        ans = (ans < fo) ? ans : fo;\n    }\n\n    // ... Repeat the rest of the code similarly\n    // ...\n\n    return ans;\n}\n\n\n\n\n```\n```Java []\nimport java.util.Arrays;\n\nclass Solution {\n    boolean check(int t) {\n        String temp = Integer.toString(t);\n        int i = 0, j = temp.length() - 1;\n        while (i < j) {\n            if (temp.charAt(i) != temp.charAt(j)) {\n                return false;\n            }\n            i++;\n            j--;\n        }\n        return true;\n    }\n    \n    long minimumCost(int[] nums) {\n        Arrays.sort(nums);\n        int n = nums.length;\n        int first = nums[n / 2];\n        int second = nums[n / 2];\n        long ans = Long.MAX_VALUE;\n\n        if (n % 2 == 0) {\n            second = nums[(n + 1) / 2];\n        }\n\n        int temp = -1;\n\n        for (int q = first; q <= second; ++q) {\n            if (check(q)) {\n                temp = q;\n                break;\n            }\n        }\n\n        if (temp != -1) {\n            long fo = 0;\n            for (int i = 0; i < n; ++i) {\n                fo += Math.abs(nums[i] - temp);\n            }\n            ans = Math.min(ans, fo);\n        }\n\n        // ... Repeat the rest of the code similarly\n        // ...\n\n        return ans;\n    }\n}\n\n\n\n```\n```python3 []\nclass Solution:\n    def check(self, t):\n        temp = str(t)\n        i, j = 0, len(temp) - 1\n        while i < j:\n            if temp[i] != temp[j]:\n                return False\n            i += 1\n            j -= 1\n        return True\n    \n    def minimumCost(self, nums):\n        nums.sort()\n        n = len(nums)\n        first = nums[n // 2]\n        second = nums[n // 2]\n        ans = float(\'inf\')\n\n        if n % 2 == 0:\n            second = nums[(n + 1) // 2]\n\n        temp = -1\n\n        for q in range(first, second + 1):\n            if self.check(q):\n                temp = q\n                break\n\n        if temp != -1:\n            fo = 0\n            for i in range(n):\n                fo += abs(nums[i] - temp)\n            ans = min(ans, fo)\n\n        # ... Repeat the rest of the code similarly\n        # ...\n\n        return ans\n\n\n\n```\n```javascript []\nclass Solution {\n    check(t) {\n        const temp = t.toString();\n        let i = 0, j = temp.length - 1;\n        while (i < j) {\n            if (temp[i] !== temp[j]) {\n                return false;\n            }\n            i++;\n            j--;\n        }\n        return true;\n    }\n    \n    minimumCost(nums) {\n        nums.sort((a, b) => a - b);\n        const n = nums.length;\n        let first = nums[Math.floor(n / 2)];\n        let second = nums[Math.floor(n / 2)];\n        let ans = Number.MAX_SAFE_INTEGER;\n\n        if (n % 2 === 0) {\n            second = nums[(n + 1) / 2];\n        }\n\n        let temp = -1;\n\n        for (let q = first; q <= second; ++q) {\n            if (this.check(q)) {\n                temp = q;\n                break;\n            }\n        }\n\n        if (temp !== -1) {\n            let fo = 0;\n            for (let i = 0; i < n; ++i) {\n                fo += Math.abs(nums[i] - temp);\n            }\n            ans = Math.min(ans, fo);\n        }\n\n        // ... Repeat the rest of the code similarly\n        // ...\n\n        return ans;\n    }\n}\n\n\n\n```\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['String', 'C', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript']