✅ Java | easy to understand solution
Steps\n\n- Sort the array\n- Find the middle element of the array\n- Find the left and right nearest palidromic element from the mid element\n- Find answers for
# Steps\n```\n- Sort the array\n- Find the middle element of the array\n- Find the left and right nearest palidromic element from the mid element\n- Find answers for both\n- Choose the smallest as ans\n```\n\n\n# Code\n```\nboolean checkPalindrome(int n) { \n\tint reverse = 0; \n\tint temp = n; \n\twhile (temp != 0) { \n\t  reverse = (reverse * 10) + (temp % 10); \n\t  temp = temp / 10; \n\t} \n\treturn (reverse == n);\n  } \n\nprivate int[] find(int val) {\n\tint res[] = new int[2];\n\tres[0] = res[1] = -1;\n\n\tint i=val, j=val+1;\n\tboolean flag = false;\n\n\twhile(res[0] == -1){\n\t\tif(checkPalindrome(i--)) { \n\t\t\tres[0] = i+1;\n\t\t}\n\t}\n\twhile(res[1] == -1){\n\t\tif(checkPalindrome(j++)) { \n\t\t\tres[1] = j-1;\n\t\t}\n\t}\n\n\treturn res;\n}\n\npublic long minimumCost(int[] a) {\n\tArrays.sort(a);\n\tint n = a.length;\n\tint[] ans = find(a[n/2]);\n\n\tlong res1 = 0l, res2=0l;\n\tfor(int val : a){\n\t\tif(ans[0] != -1) res1 += Math.abs(val-ans[0]);\n\t\tif(ans[1] != -1) res2 += Math.abs(val-ans[1]);\n\t}\n\n\tif(ans[0] != -1 && ans[1] != -1) return Math.min(res1, res2);\n\telse if(ans[0] != -1) return res1;\n\treturn res2;\n}\n```\n\n
['Sorting', 'Java']