Company: Visa
Difficulty: easy
Sum of Differences of All Pairs You are given an integer array a of size n . For every pair of indices (i, j) with i < j , the difference of that pair is defined as |a[i] - a[j]| Compute the sum of the differences of all such pairs. Because the sum can be very large, return it modulo 10^9 + 7 . Function Description Complete the function sumOfDifferences in the editor below. sumOfDifferences has the following parameters: int n : the number of elements in the array int a[n] : the array of integers Returns long : the sum of |a[i] - a[j]| over all pairs i < j , taken modulo 10^9 + 7 . The returned value must be the canonical residue, an integer in the range 0 to 10^9 + 6 . If n is 1 there are no pairs, and the answer is 0 . Constraints 1 <= n <= 15000 0 <= a[i] <= 10^9 Input Format For Custom Testing The first line contains a single integer, n . The second line contains n space-separated integers, a[0] through a[n - 1] . Output Format Print the single returned value on it