All the Gaps — Solution & Editorial
Sort; element x at sorted index i (0-based) contributes x·i − (sum of everything before it). One pass with a running sum.
Complexity: O(N log N)
Watch out: ~1013 at the limits — 64-bit.
Sort; element x at sorted index i (0-based) contributes x·i − (sum of everything before it). One pass with a running sum.
Complexity: O(N log N)
Watch out: ~1013 at the limits — 64-bit.