Beginner-Friendly || Simple Mathematics || C++
Approach\n\nSort the array in ascending order.\n\nFind the 3 suitable palindromes for the array :\n    1) Between 0 to nums[n/2].\n    2) Between nums[n/2] to n
# Approach\n\nSort the array in ascending order.\n\nFind the 3 suitable palindromes for the array :\n    1) Between 0 to nums[n/2].\n    2) Between nums[n/2] to nums[n - 1].\n    3) Nearest palindrome greater than nums[n - 1].\n\nif nums[n/2] number is a Palindrome we take that number for further calculations as well.\n\nFrom the 4 obtained palindromes, calculate each score in 4 variable and return the minimum from them.\n\n# Code\n```\ntypedef long long int ll;\n\nclass Solution {\nprivate:\n    bool isPalindrome(int x){\n        string s = to_string(x);\n        ll n = s.size();\n        for(ll i=0; i<n/2; i++)\n            if(s[i] != s[n - i - 1]) return false;\n\n        return true;\n    }\n\npublic:\n    long long minimumCost(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        ll n = nums.size();\n        ll x = nums[n/2];\n\n        ll pn = nums[n/2];\n\n\n        ll pn1 = -1, pn2 = -1;\n        ll pn3 = -1;\n\n        if(!isPalindrome(pn)){\n\n            for(ll i = pn ; i <= nums[n - 1]; i++){\n                if(isPalindrome(i)){\n                    pn1 = i;\n                    break;\n                }\n            }\n            \n            for(ll i = pn; i >= 0; i--){\n                if(isPalindrome(i)){\n                    pn2 = i;\n                    break;\n                }\n            }\n\n            ll i = nums[n - 1];\n\n            while(true){\n                if(isPalindrome(i)){\n                    pn3 = i;\n                    break;\n                }\n                i++;\n            }\n            pn = -1;\n        }\n\n\n        ll sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;\n\n        bool flag1 = false, flag2 = false, flag3 = false, flag4 = false;\n\n        if(pn1 != -1) flag1 = true;\n        if(pn2 != -1) flag2 = true;\n        if(pn3 != -1) flag3 = true;\n        if(pn != -1) flag4 = true;\n\n        for(ll i=0; i<n; i++){\n            if(flag1) sum1 += abs(nums[i] - pn1);\n            if(flag2) sum2 += abs(nums[i] - pn2);\n            if(flag3) sum3 += abs(nums[i] - pn3);\n            if(flag4) sum4 += abs(nums[i] - pn);\n        }\n        \n        ll ans = LLONG_MAX;\n\n        if(flag1 && ans > sum1) ans = sum1;\n        if(flag2 && ans > sum2) ans = sum2;\n        if(flag3 && ans > sum3) ans = sum3;\n        if(flag4 && ans > sum4) ans = sum4;\n\n        return ans;\n    }\n};\n```
['Array', 'Math', 'Greedy', 'Sorting', 'C++']