Lucid Clear Code ||Find All the Possible Palindromic Number Near the Median||EDGE CASE||C++||
\n# Approach\n- We have to sord and find the Median of the Array and the digits of the digits are stored in a vector for further use;\n\n- There exist 5 possibl
\n# Approach\n- We have to sord and find the Median of the Array and the digits of the digits are stored in a vector for further use;\n\n- There exist 5 possible cases for a nearest possible palindromic number.\nexample-4723\nCase 1 \u2013 The next closest palindrome has one digit extra : So here it will be 10001.\nCase 2 \u2013 The next closest palindrome has one digit less: So here it will be 999.\nCase 3 \u2013 The next closest palindrome has the same number of digits.\nFor case 3 there are 3 subcases:\n\n    The middle digit remains same. Postfix is the mirror image of   prefix. So here 47(prefix) \u2013 74(postfix)\u2013>4774.\n    The middle digit increases by 1. Postfix is the mirror image of prefix. So here 4884.\n    The middle digit decreases by 1. Postfix is the mirror image of prefix. So here 4664.\n\n---\nEDGE CASE\nWe will use all the 5 possible numbers to compute our least answer.(We are not finding the palindromic number that is closest to the median because 2 palindromic number can be of same difference from the median e.g 111 and 121 both have same difference from 116, using one to compute the answer can result in faulty answers as while computing the palindromic number we are not concerened about the content of the array.)\n\nAs we have generated 5 palindromic numbers so there are 5 possible answers , we will return the minimum of it .\n\nSimple:)\n\n\n# Code\n```\nclass Solution {\npublic:\n    void nearestPalnidrome(vector<long long int>&prefer,vector<int>&digits,int median){\n        if(digits.size()==1){\n            prefer.push_back(digits[0]);\n            return ;\n        }\n        int middle;\n        if(digits.size()%2==0)\n        {middle=digits.size()/2;}\n        else middle=(digits.size()+1)/2;\n        // cout<<middle<<"   "; \n        prefer.push_back(pow(10,digits.size())+1);\n        prefer.push_back(pow(10,digits.size()-1)-1);\n        long long int num1=0,num2=0,num3=0;\n        for(int i=0;i<middle;i++){\n            num1+=digits[i]*pow(10,middle-i-1);   \n        }\n        num2=num1+1;\n        num3=num1-1;\n        cout<<num1<<" "<<num2<<" "<<num3<<" ";\n        vector<long long int>n1(middle,0),n2(middle,0),n3(middle,0);\n        int a=0,b=0,c=0;\n        while(num1>0){\n            n1[a]=(num1%10);\n            num1=num1/10;\n            a++;\n        }\n        while(num2>0){\n            n2[b]=(num2%10);\n            num2=num2/10;\n            b++;\n        }\n        while(num3>0){\n            n3[c]=(num3%10);\n            num3=num3/10;\n            c++;\n        }\n        reverse(n1.begin(),n1.end());\n        reverse(n2.begin(),n2.end());\n        reverse(n3.begin(),n3.end());\n\n        long long int candi1=0,candi2=0,candi3=0;\n        int k=digits.size()-1;\n        for(int i=0;i<n1.size();i++){\n            candi1+=n1[i]*pow(10,k);\n            candi2+=n2[i]*pow(10,k);\n            candi3+=n3[i]*pow(10,k);\n            k--;\n        }\n        cout<<k<<"*"<<candi1<<" "<<candi2<<" "<<candi3<<" ";\n        if(digits.size()%2==1) middle--;\n        for(int i=middle-1;i>=0;i--){\n            candi1+=n1[i]*pow(10,k);\n            candi2+=n2[i]*pow(10,k);\n            candi3+=n3[i]*pow(10,k);\n            k--;\n        }\n        prefer.push_back(candi1);\n        prefer.push_back(candi2);\n        prefer.push_back(candi3);\n\n        return ;\n    }\n    long long minimumCost(vector<int>& nums) {\n        int n=nums.size();\n        sort(nums.begin(),nums.end());\n        int median,mid;\n        median = (n %2 )?nums[n/2]:(nums[n/2] + nums[n/2 - 1])/2;\n        mid=median;\n        vector<int>digits;\n        \n        while(mid>0){\n            digits.push_back(mid%10);\n            mid=mid/10;\n        } \n        reverse(digits.begin(),digits.end());\n        vector<long long int>prefer;\n        nearestPalindrome(prefer,digits,median);\n        vector<long long int>pali(prefer.size(),0);\n        for(int j=0;j<prefer.size();j++){\n            cout<<prefer[j]<<" ";\n            for(int i=0;i<n;i++){\n                pali[j]+=abs(nums[i]-prefer[j]);\n            }\n        }\n        sort(pali.begin(),pali.end());\n\n        return pali[0];\n\n    }\n};\n```
['C++']