Problem statement

https://binarysearch.com/problems/Every-Pair-of-Absolute-Difference/

Solution

The idea is for each value in sorted array calculate how many times it is used.

Complexity

Time complexity is O(n log n), space is O(n)

Code

class Solution:
    def solve(self, nums):
        nums = sorted(nums)
        n = len(nums)
        return (sum(x*y for x, y in zip(nums, range(-n+1,n, 2))) * 2) % (10**9 + 7)