I want to find the best common time to change the other times the least (that the sum of the differences is the lowest possible).
On input, I have the array of times.
On output, there should be the new, common time.
Note that the times can be not ordered and be absolutely different (e.g. 02:00, 02:30 and 03:30)
Example 1
Input: ["01:00", "02:00", "03:00", "04:00", "05:00"]
Output should be 03:00
, because it's the best to change 01:00 to 03:00 (2 hours of change), 02:00 to 03:00 (1 hour of change), keep 03:00 the same, 04:00 to 03:00 (1 hour), and 05:00 to 03:00 (2 hours).
Example 2
Input: ["12:00", "13:00", "14:00"]
Output should be 13:00
- change 12:00 to 13:00 (1 hour) and 14:00 to 13:00 (1 hour)
Example 3 (tricky)
Input: ["23:00", "01:00", "02:00"]
Output should be 01:00
- change 23:00 to 01:00 (2 hours) and 02:00 to 01:00 (1 hour) (the tricky thing is 23:00 - the best time is not 13:00)
I tried functions that make the average of times, e.g. https://stackoverflow.com/a/52839039/19022995, but unfortunately it doesn't work
I would be really grateful for tips how to do this
Thank you in advance