#gujju_ben_ni_mazza #
Intuition\n Describe your first thoughts on how to solve this problem. \nBadha number ne mare to koi ek j palindromic number na andar convert karvanoo 6e...\nto
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBadha number ne mare to koi ek j palindromic number na andar convert karvanoo 6e...\nto akha array na badha j numbers koi ek palindromic number na andar convert thai jase then aene kehvase => equilindromic array....\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nmain catch ae hato ke tamare median na according answer ne find out karvano 6e and then check ke kevi ritna aeno answer find out thai 6e...\nsort kari do badha nums ne ....\nand then \nif **n==1** => base case j 6e then return 0...\nor if n%2==1 => odd number mate to you get the median easily...\nor else even na case ma you take the average of the middle elements...\n\nthen check whether that element middle is palindromic or not => if it is palindromic then we need to calculate the cost for it and store it in our ans...\n\n\nthen do the binary search ke aena middle na agad pachad koi values made 6e ke nahi...\nbanne baju left/right je pan baju tmne palindromic number made aene to just save karido and don\'t go more backward => we just need the just nearest palindromic number for our answers from both the sides.\n\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nsorting the array : O(log(n))\ncalculate the cost: O(n)\nfind the middle : O(log(n))\n======\noverall time complexity : O(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nno extra space is used : O(1)\n\n# Code\n```\nclass Solution {\n    public static boolean is_palindrome(int num){\n        String str=String.valueOf(num);\n        String rev= new StringBuilder(str).reverse().toString();\n// System.out.println("str => "+ str+"  "+str.equals(rev));\n        return str.equals(rev);\n    }\n    public static long calculate_cost(int nums[],int target){\n        long ans=0;\n// System.out.println("target : "+target);\n        for(int i=0;i<nums.length;i++){\n            ans+= Math.abs(target- nums[i]);\n        }\n//         System.out.println("ans : "+ans);\n//  System.out.println("------------------");\n        return ans;\n    }\n    public long minimumCost(int[] nums) {\n        /*the main catch of the whole problem is to get the median and calculate the result. */\n        int n=nums.length;\n        long ans=Long.MAX_VALUE;\n        int middle=0;\n        Arrays.sort(nums);\n        if(n==1){return 0;}\n        if(n%2==1){\n            middle= nums[n/2];\n        }else {\n            middle=(nums[n/2]+nums[(n+1)/2])/2;            \n        }\n\n        // System.out.println("middle: "+middle);\n\n        /*if the middle is palindromic then just calculate the cost and return the answer  */\n        if(is_palindrome((int)middle)){\n            ans=calculate_cost(nums,(int)middle);\n            // System.out.println("calculate the cost for this middle : "+ans);\n        }\n\n        /*now get the palindromic numbers from the left and right and calculat ethe result. */\n        long low=middle-1, high=middle+1;\n        long left_pal=-1, right_pal=-1;\n/*repeat this process till we haven\'t got any left palindrom or any right palindrom */\n        while(left_pal==-1 ||right_pal==-1){\n            if(left_pal==-1 && is_palindrome((int)low)){\n                left_pal=low;\n            }\n            if(right_pal==-1 && is_palindrome((int)high)){\n                right_pal=high;\n            }\n            low--;high++;\n        }\n        \n\n        return Math.min(\n            ans, \n        Math.min(calculate_cost(nums,(int)left_pal),calculate_cost(nums,(int)right_pal)));\n    }\n}\n```
['Java']