96.7% Pass Easy Solution | 🎁🎁🎁🎁
Intuition\n Describe your first thoughts on how to solve this problem. \nWhat should we need to find mean or median?( Hint: Median )\nAnd after finding the targ
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhat should we need to find **mean** or **median**?( $$Hint: Median$$ )\nAnd after finding the target: \nWe need to Find near Paindromic numbers. Yes, not number, it should be numbers. We have to find 2 Palindromic numbers. One should be greater than target and other one should be lower than target.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo Solve this problem we need to solve three task efficiently:\n1. Find the median.\n2. Find the Palindromic numbers\n3. Calculate the difference\n\n- For first task sort the array and find the middle element( odd number of elements) or avg of middle elements( even number of elements).\n- For second task we need to find the palidromic numbers.\n    - Convert the median to string\n    - check for palindrome ( if median is palindrome we do not need to find further. )\n    - If not, Convert a string to palindrome: What should be the nearest palindrome of 5467. It is 5445, what we did is copy the half string to half end.\n    - Now when we get one palindromic number we need to find another.\n    - Check that current palindromic number is less than or greater than median.( $$e.g.$$ 5432 - 5445 and 5467 - 5445).\n    - If greater than decrease the middle char by 1 otherwise increase the middle char by 1.\n    - $$Example$$: 5467 - find palindrome = 5445. 5445 < 5467, So we need to increase the middle char. middle position is 1: 4 in 5467. number become 5567. convert into palindrome - 5555. So for number 5467 : 5445 & 5555 are nearest palindrome.\n    - **Note**: We need to prevent 2 cases:\n        - if middle pos have \'0\' when you need to decrease it or middle pos have \'9\' when you need to increase it.\n        - Other case is when median is 10: than this fail because palindrome are 9 and 11.\n- After calculating Two palindrome we need to calculate the difference. Run a for loop and calculate the absolute difference and return the minimum one.\n\n**IF YOU LIKE THE APPROACH, Please LIKE THIS POST. \u2764**\n# Code\n```\nclass Solution {\npublic:\n    long long minimumCost(vector<int>& nums) {\n        int median; // store median of nums array\n        \n        sort( nums.begin(), nums.end() );// sort array \n        \n        if( nums.size() % 2 == 0 )\n            median = nums[nums.size() / 2];\n        else\n            median = (nums[nums.size() / 2] + nums[(nums.size() - 1) / 2]) / 2;\n        \n        string temp = to_string( median ); // convert into string\n        vector<int> target( 2, 0); // to store palindromic numbers\n        int a = temp.length() / 2; // mid pos\n\n        //check current number is palindromic or not\n        if(temp.length() == 1 || temp.substr( 0, a + 1) == temp.substr(a, a + 1) )\n            target[0] = median;\n        \n        else{\n            // if median is 10 case.\n            if( median == 10){\n                int z = lower_bound( nums.begin(), nums.end() ,10) - nums.begin();\n                if( z != 0 )\n                    target = {nums[z-1], 11};\n                else\n                    target = {9,11};   \n            }\n            else{\n\n                string x = temp; // for first palindrome\n                string y = temp; //for second palindrome\n                \n                //convert x string into palindrome\n                while( a-- ) x[x.size() - a - 1 ] = x[a];\n                \n                int pos = temp.length() / 2;\n                int temp1 = stoi( x, 0, 10); // convert into (int)\n\n                //check for greater \n                if( temp1 > median ){\n                    // check for length of string( even or odd )\n                    if( temp.length() % 2 == 0 ){\n                        pos--; \n                    }\n                    //if current char is \'0\'\n                    while( y[pos]==\'0\'){\n                        y[pos] = \'9\';\n                        pos--;\n                    }\n                    //decrease the char by 1.\n                    y[pos] = y[pos] - 1;\n                }\n                else{\n\n                    if( temp.length() % 2 == 0 ){\n                    pos--; \n                    }\n                    while( y[pos]==\'9\'){\n                        y[pos] = \'0\';\n                        pos--;\n                    }\n                    y[pos] = y[pos] + 1;\n                }\n                \n                //convert the y string into palindrome\n                a = temp.length() / 2;\n                while( a-- ){\n                    y[y.size() - a - 1 ] = y[a];\n                }\n                int temp2 =  stoi( y, 0, 10);\n                target = {temp1, temp2}; // store result\n            }\n        }\n\n        vector<long long> ans(2,0); // to store ans\n        for( auto& i : nums){\n            ans[0] += abs(i - target[0]);\n            ans[1] += abs(i - target[1]);\n        }\n        return ans[0]<ans[1] ? ans[0]: ans[1]; // return minimum\n    }\n};\n```
['C++']