Problem · Array
Sum of Index-Ordered Pair Differences
Learn this problemProblem statement
You are given an integer array arr of length n.
For every pair of indices (i, j) such that 0 <= i < j < n, compute:
arr[j] - arr[i]
Find the sum of these differences over all valid pairs. Return the result modulo 10^9 + 7.
Complete the function sumPairDifferences, which accepts arr and returns the required result. Here, n = arr.length.
Function
sumPairDifferences(arr: int[]) → intExamples
Example 1
arr = [1, 2, 3]return = 4Here, n = 3. All pair differences are:
2 - 1 = 13 - 1 = 23 - 2 = 1
The sum is 1 + 2 + 1 = 4, and 4 modulo 10^9 + 7 is 4.
Example 2
arr = [4, 1, 3, 2]return = 1000000003Here, n = 4. All pair differences are:
1 - 4 = -33 - 4 = -12 - 4 = -23 - 1 = 22 - 1 = 12 - 3 = -1
The sum is (-3) + (-1) + (-2) + 2 + 1 + (-1) = -4, and -4 modulo 10^9 + 7 is 1000000003.
Constraints
1 <= n <= 2 * 10^50 <= arr[i] <= 10^9
More Visa problems
- Maximum Server Processing TimeOA · Seen Jul 2026
- Signal PingsOA · Seen Jul 2026
- Minimum Cost to Attend Required CoursesOA · Seen Jul 2026
- Minimum Score of a Path Between CitiesOA · Seen Jul 2026
- Planning ProductionOA · Seen Jul 2026
- Subarray SumOA · Seen Jul 2026
- Maximum Even Tag SumOA · Seen Jun 2026
- Transform Binary MatrixOA · Seen Jun 2026