Very simple Approach 🔥|| Find the nearest palindrome to the middle element
Intuition\nIn this problem we just need to make all the elements in an array a palindrome number. We can solve it by making all the elements equal to a palindro
# Intuition\nIn this problem we just need to make all the elements in an array a palindrome number. We can solve it by making all the elements equal to a palindrome nearest to the one in the mid of the original array given.\n\n# Approach\nFind the nearest palindrome to the middle element of the original array. We can iterate in both the sides of the middle element and find the nearest of all the palindromes for our cost to be minimum as much.\n\n\n# Code\n```\nclass Solution {\npublic:\n    bool palindrome(int n){\n        vector<int> nums;\n        while(n != 0){\n            nums.push_back(n % 10);\n            n /= 10;\n        }\n        reverse(nums.begin(), nums.end());\n        int m = nums.size();\n        for(int i = 0; i <= m/2; i++){\n            if(nums[i] != nums[m - 1 - i]){\n                return false;\n            }\n        }\n        return true;\n    }\n    long long minimumCost(vector<int>& nums) {\n        int n = nums.size();\n        long long totalCost = 0;\n        sort(nums.begin(), nums.end());\n        if(palindrome(nums[n/2])){\n            for(int i = 0; i < n; i++){\n                totalCost += abs(nums[i] - nums[n/2]);\n            }\n            return totalCost;\n        }\n        int ans1 = INT_MAX;\n        int ans2 = INT_MAX;\n        for(int i = nums[n/2] -1; i >= 1; i--){\n            if(palindrome(i)){\n                ans1 = i;\n                break;\n            }\n        }\n        for(int i = nums[n/2] + 1; i <= INT_MAX; i++){\n            if(palindrome(i)){\n                ans2 = i;\n                break;\n            }\n        }\n        int mini;\n        totalCost = 0;\n        long long totalCost1 = 0;\n        long long totalCost2 = 0;\n        for(int i = 0; i < n; i++){\n            totalCost1 += abs(nums[i] - ans1);\n        }\n        for(int i = 0; i < n; i++){\n            totalCost2 += abs(nums[i] - ans2);\n        }\n        totalCost = min(totalCost1, totalCost2);\n        return totalCost;\n    }\n};\n```
['Array', 'Math', 'Brainteaser', 'Sorting', 'C++']