
Sum of Absolute Differences in a Sorted Array (medium)
Problem Statement
Given an integer array nums sorted in increasing order, return an array result of the same length, where result[i] should be the sum of the absolute differences between nums[i] and every other element in nums.
Examples
Example 1
- Input: [1, 3, 6]
- Output: [7, 5, 8]
- Explanation:
- For result[0]: |1-3| + |1-6| = 2 + 5 = 7
- For result[1]: |3-1| + |3-6| = 2 + 3 = 5
- For result[2]: |6-1| + |6-3| = 5 + 3 = 8
Example 2
- Input: [2, 4, 7]
- Output: [7, 5, 8]
- Explanation:
- For result[0]: |2-4| + |2-7| = 2 + 5 = 7
- For result[1]: |4-2| + |4-7| = 2 + 3 = 5
- For result[2]: |7-2| + |7-4| = 5 + 3 = 8
Example 3
- Input: [1, 2, 4, 5]
- Output: [8, 6, 6, 6]
- Explanation:
- For result[0]: |1-2| + |1-4| + |1-5| = 1 + 3 + 4 = 8
- For result[1]: |2-1| + |2-4| + |2-5| = 1 + 2 + 3 = 6
- For result[2]: |4-1| + |4-2| + |4-5| = 3 + 2 + 1 = 6
- For result[3]: |5-1| + |5-2| + |5-4| = 4 + 3 + 1 = 8
Constraints:
- 2 <= nums.length <= 10<sup>5</sup>
- 1 <= nums[i] <= nums[i + 1] <= 10<sup>4</sup>
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory