Rust 100% - math explanation
Approach\n Describe your approach to solving the problem. \nI\'ll use the notation ans for the array returned by the function of len n = nums.len().\n\nWe notic
# Approach\n<!-- Describe your approach to solving the problem. -->\nI\'ll use the notation `ans` for the array returned by the function of len `n = nums.len()`.\n\nWe notice that the absolute difference of two numbers `a` and `b` is equal to `max(a, b) - min(a, b)`. By notifing that and the fact that the `nums` array is sorted we can come up with an expression to calculate `ans[i]` :\n$$ans[i] = (\\sum_{0}^{i-1} nums\\left [ i \\right ] - nums\\left [ j \\right ]) + (\\sum_{i+1}^{n-1} nums\\left [ j \\right ] - nums\\left [ i \\right ])$$\n\nBy rearanging terms we can get to the following equality :\n$$ans\\left [ i \\right ] = nums\\left [ i \\right ] \\times  - (\\sum_{0}^{i-1}nums\\left [ j \\right ]) + (\\sum_{i+1}^{n-1} nums\\left [ j \\right ]) - nums\\left [ i \\right ] \\times (n - i - 1)$$\n\nNow the next step is to try to find a recursion between $$ans[i]$$ and $$ans[i-1]$$. To do so we can simply calculate $$ans[i] - ans[i-1]$$. Using the previous expression we found we can obtain the following result :\n$$ans\\left [ i \\right ] = ans\\left [ i - 1 \\right ] + (nums\\left [ i \\right ] - nums\\left [ i - 1 \\right ]) \\times (2\\times i - n)$$\n\nThis is exactly what we were looking for. Now we can simply initialize an array with the first value and iterate throught `nums` and calculate each `ans[i]` with the recursive relation.\n\n# Algorithm\n1. Create an array `ans` of size `n = nums.len()`\n2. Initialize `ans[0] = nums.iter().sum() - nums[0] * nums.len() as i32`\n3. Iterate from `1..nums.len()`\n    - Update `ans[i]` to `ans[i - 1] + (nums[i] - nums[i - 1]) * (2*i - n)`\n4. Return `ans` \n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimpl Solution {\n    pub fn get_sum_absolute_differences(nums: Vec<i32>) -> Vec<i32> {\n        let mut ans = vec![0; nums.len()];\n        ans[0] = nums.iter().sum::<i32>() - nums[0] * nums.len() as i32;\n\n        for i in 1..nums.len() {\n            ans[i] = ans[i - 1] + (nums[i] - nums[i - 1]) * (2 * i as i32 - nums.len() as i32);\n        }\n\n        ans\n    }\n}\n```
['Math', 'Rust']