i holds values[i] boxes. The manager wants every shelf to hold the **same** number of boxes, and that number must be a palindrome: a positive integer that reads the same forwards and backwards (for example 7, 44 or 1331).
Adding or removing a single box costs 1, so turning a shelf with values[i] boxes into one with y boxes costs |values[i] − y|.
Return the minimum total cost to make all shelves hold the same palindromic number of boxes.values=[7, 9, 15] → 8
The median is 9, which is a palindrome. Setting everything to 9 costs |7 − 9| + |15 − 9| = 2 + 6 = 8.
values=[20, 25, 30] → 13
The palindromes closest to the median 25 are 22 and 33. Going to 22 costs 2 + 3 + 8 = 13, going to 33 costs 13 + 8 + 3 = 24. The answer is 13.
values=[44] → 0
values=[7, 9, 15]
8
Run checks the first 3 tests in your browser. Submit checks all 119 tests on the server, hidden ones included.