Sunday common C++ interview question: Minimum cost to reach an equal array
Given two arrays (numbers and costs) of positive integers, determine the minimum cost to adjust the numbers array so that all elements are of the same value.
The cost of adjusting each element by one is the corresponding element in the costs array.
Solve it yourself: https://compiler-explorer.com/z/dedx8Yd68
Solution: https://compiler-explorer.com/z/7zzWEjT5K
@simontoth Are we permitted to reorder the elements of either array without also adjusting the corresponding array? What is the cost of such a reorder step?
@mikemol You are not trying to match the first array to the second, you are trying to adjust the values in the first array, and the second array is just giving you the cost.
The order of elements (as long as you keep them matched up with their costs) doesn't matter.
@simontoth Understood, but sometimes I find I can shift operations into a context where they're cheaper (time domain, space domain, cost domain...), and that's what I was probing with that question.
@mikemol Well, feel free to.
I meant that you cannot change the costs by re-ordering elements.
But if re-ordering helps, then have at it.
@simontoth And that answers my question, since I sought to reduce cost by temporarily changing the value/cost association during intermediate steps, before returning them to their original associations. :)