1685. Sum of Absolute Differences in a Sorted Array Medium
1/**
2 * [1685] Sum of Absolute Differences in a Sorted Array
3 *
4 * You are given an integer array nums sorted in non-decreasing order.
5 * Build and return an integer array result with the same length as nums such that result[i] is equal to the summation of absolute differences between nums[i] and all the other elements in the array.
6 * In other words, result[i] is equal to sum(|nums[i]-nums[j]|) where 0 <= j < nums.length and j != i (0-indexed).
7 *
8 * Example 1:
9 *
10 * Input: nums = [2,3,5]
11 * Output: [4,3,5]
12 * Explanation: Assuming the arrays are 0-indexed, then
13 * result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,
14 * result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,
15 * result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.
16 *
17 * Example 2:
18 *
19 * Input: nums = [1,4,6,8,10]
20 * Output: [24,15,13,15,21]
21 *
22 *
23 * Constraints:
24 *
25 * 2 <= nums.length <= 10^5
26 * 1 <= nums[i] <= nums[i + 1] <= 10^4
27 *
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/
32// discuss: https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37 pub fn get_sum_absolute_differences(nums: Vec<i32>) -> Vec<i32> {
38 vec![]
39 }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn test_1685() {
50 }
51}
52
Back
© 2025 bowen.ge All Rights Reserved.