Alright, I'll assume that we want to minimize the sum of differences over all groups.
Let's sort the numbers. There's an optimal answer where each group is a consecutive segment in the sorted array (proof: let A1 < B1 < A2 < B2. We can exchange A2 and B1. The answer will not increase).
Let a[l], a[l + 1], ..., a[r] is a group. It's cost is a[r] - a[l] = (a[r] - a[r - 1]) + (a[r - 1] - a[r - 2]) + ... + (a[l + 1] - a[l])
. It leads us to a key insight: k
groups is k - 1
gaps and the answer is a[n - 1] - a[0] - sum of gaps
. Thus, we just need to maximize the gaps.
Here is a final solution:
- sort the array
- compute differences between adjacent numbers
- take
k - 1
largest differences. That's exactly where the groups split.
- We can find the
k-1
th largest element in linear time (or if we are fine with O(N log N)
time, we can just sort them). That's it.
Here is an example:
x = [1, 1, 4, 3], k = 2
sorted: [1, 1, 3, 4]
differences: [0, 2, 1]
taking k - 1 = 1
largest gaps: it's 2. Thus the groups are [1, 1]
and [3, 4]
.
A slightly more contrived one:
x = [8, 2, 0, 3], k = 3
sorted: [0, 2, 3, 8]
differences: [2, 1, 5]
taking k - 1 = 2
largest gaps: they're 2 and 5. Thus, the groups are [0], [2, 3], [8]
with the total cost of 1.