Amazon interview question

Minimum sum of distances coding question