Simple C++ Solution || Easy to understand || Median
Intuition\nThe problem involves finding the minimum cost to transform an array into a palindrome by adjusting the values of its elements.\nInitially, the approa
# Intuition\nThe problem involves finding the minimum cost to transform an array into a palindrome by adjusting the values of its elements.\nInitially, the approach considers checking the cost for each element being a palindrome and then explores nearby values around the median.\n\n# Approach\n 1. Sort the given array to simplify calculations.\n 2. Initialize the minimum cost (`ans`) to a maximum value.\n 3. Find the median of the sorted array.\n 4. Iterate through the array and check each element for palindrome properties. Update the minimum cost accordingly.\n 5. Explore nearby values around the median, considering them as potential solutions and updating the minimum cost.\n 6. Return the minimum cost as the result.\n\n# Complexity\n- Time complexity: O(n^2)\n\n# Code\n```cpp\nclass Solution {\npublic:\n    // Function to check if a number is a palindrome\n    bool isPalindrome(long long n) {\n        long long orgNum = n;\n        long long revNum = 0;\n\n        // Reverse the number to check for palindrome\n        while (n > 0) {\n            long long digit = n % 10;\n            revNum = revNum * 10 + digit;\n            n /= 10;\n        }\n\n        return orgNum == revNum;\n    }\n\n    // Function to calculate the sum of absolute differences from a target value\n    long long calsum(const vector<int>& nums, long long target) {\n        long long ans = 0;\n        for (long long i = 0; i < nums.size(); i++) {\n            ans += abs(nums[i] - target);\n        }\n        return ans;\n    }\n\n    // Function to find the minimum cost of making the array a palindrome\n    long long minimumCost(vector<int>& nums) {\n        // Sort the array to simplify calculations\n        sort(nums.begin(), nums.end());\n\n        long long ans = LLONG_MAX;\n        int n = nums.size();\n\n        // Use the median instead of the average\n        long long median = nums[n/2];\n\n        // Iterate through the array and check each element for palindrome properties\n        for (int i = 0; i < n; i++) {\n            if (isPalindrome(nums[i])) {\n                ans = min(ans, calsum(nums, nums[i]));\n            }\n        }\n\n        // Explore nearby values around the median\n        long long f = median - 1;\n        long long s = median + 1;\n        bool flag1 = false, flag2 = false;\n\n        while (!flag1 || !flag2) {\n            long long ans1 = LLONG_MAX, ans2 = LLONG_MAX;\n\n            // Check if the lower value is a palindrome\n            if (f >= 0 && isPalindrome(f) && !flag1) {\n                flag1 = true;\n                ans1 = calsum(nums, f);\n            }\n\n            // Check if the higher value is a palindrome\n            if (isPalindrome(s) && !flag2) {\n                flag2 = true;\n                ans2 = calsum(nums, s);\n            }\n\n            // Update the minimum cost correctly\n            ans = min(ans, min(ans1, ans2));\n\n            f--;\n            s++;\n        }\n\n        return ans;\n    }\n};\n
['C++']