✅☑[C++/Java/Python/JavaScript] ||  Beats 99% || 2 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1\n1. vector getSumAbsoluteDifferences(vector& nums): A funct
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1***\n1. **vector<int> getSumAbsoluteDifferences(vector<int>& nums):** A function that takes a vector of integers as input and returns a vector that contains the sum of absolute differences for each element of the input vector.\n\n1. **int n = nums.size();:** Determines the size of the input vector nums.\n\n1. **int A = 0, B = accumulate(nums.begin(), nums.end(), 0);:** Initializes two variables `A` and `B`. `A` starts at 0, while `B` is set to the sum of all elements in `nums`.\n\n1. **vector<int> ans(n, 0);:** Initializes a result vector `ans` of size `n`, filled with zeros.\n\n1. **#pragma unroll:** Compiler directive to suggest loop unrolling for optimization purposes.\n\n1. **for(int x : nums) { ... }:** Looping through each element `x` in the `nums` vector.\n\n1. **ans[i++] = (2 * i - n) * x + B - A;:** Calculates and assigns the sum of absolute differences based on the given formula to the `i`th position in the `ans` vector.\n\n1. **A += x;** and **B -= x;:** Updates the values of `A` and `B` by adding and subtracting the current element `x`, respectively.\n\n1. Returns the `ans` vector containing the sum of absolute differences for each element in the input vector `nums`.\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int n = nums.size();\n        int A = 0, B = accumulate(nums.begin(), nums.end(), 0);\n        vector<int> ans(n, 0);\n        int i = 0;\n        \n        // Loop through the elements of the \'nums\' vector\n        // Unrolls the loop for optimization\n        #pragma unroll\n        for(int x : nums){\n            // Calculate and store the absolute differences based on the formula\n            ans[i++] = (2 * i - n) * x + B - A;\n            A += x;\n            B -= x;\n        }\n        return ans; // Return the vector containing the sum absolute differences\n    }\n};\n\n\n\n```\n```C []\n\nint* getSumAbsoluteDifferences(int* nums, int numsSize) {\n    int n = numsSize;\n    int A = 0, B = 0;\n    int *ans = (int*)malloc(n * sizeof(int));\n\n    for (int i = 0; i < n; i++) {\n        B += nums[i];\n    }\n\n    int i = 0;\n    for (int j = 0; j < n; j++) {\n        ans[i] = (2 * i - n) * nums[j] + B - A;\n        A += nums[j];\n        B -= nums[j];\n        i++;\n    }\n\n    return ans;\n}\n\n\n\n```\n```Java []\n\n\nclass Solution {\n    public int[] getSumAbsoluteDifferences(int[] nums) {\n        int n = nums.length;\n        int A = 0, B = 0;\n        for (int x : nums) {\n            B += x;\n        }\n\n        int[] ans = new int[n];\n        int i = 0;\n        for (int x : nums) {\n            ans[i] = (2 * i - n) * x + B - A;\n            A += x;\n            B -= x;\n            i++;\n        }\n        return ans;\n    }\n}\n\n\n```\n\n```python3 []\n\n\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        n = len(nums)\n        A = B = 0\n        for x in nums:\n            B += x\n\n        ans = [0] * n\n        i = 0\n        for x in nums:\n            ans[i] = (2 * i - n) * x + B - A\n            A += x\n            B -= x\n            i += 1\n        return ans\n\n\n\n```\n```javascript []\nfunction getSumAbsoluteDifferences(nums) {\n    const n = nums.length;\n    let A = 0, B = nums.reduce((acc, curr) => acc + curr, 0);\n    const ans = [];\n\n    let i = 0;\n    for (const x of nums) {\n        ans.push((2 * i - n) * x + B - A);\n        A += x;\n        B -= x;\n        i++;\n    }\n\n    return ans;\n}\n\n```\n\n---\n\n#### ***Approach 2(Prefix Sum***\n\n\n1. **Prefix Sum Calculation:**\n\n    - A prefix array is created to store the cumulative sum of elements in `nums` up to each index.\n    - It helps in efficiently calculating the left and right sum for each element.\n1. **Iterating through Elements:**\n\n    - For each element `nums[i]` in the input array:\n1. **Calculating Left and Right Sums:**\n\n    - **leftSum:** The sum of elements to the left of the current element `nums[i]`.\n    - **rightSum:** The sum of elements to the right of the current element `nums[i]`.\n1. **Count of Elements:**\n\n    - **leftCount:** The count of elements to the left of the current element `nums[i]`.\n    - **rightCount:** The count of elements to the right of the current element `nums[i]`.\n1. **Absolute Difference Sums:**\n\n    - **leftTotal:** The sum of absolute differences for the left part based on leftSum and leftCount.\n    - **rightTotal:** The sum of absolute differences for the right part based on rightSum and rightCount.\n1. **Total Sum of Absolute Differences:**\n\n    - **leftTotal + rightTotal:** Summing the left and right absolute differences to get the total sum for the current element.\n1. **Result:**\n\n    - The calculated sums are stored in the `ans` vector and returned as the final result.\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n  \n\n\n```C++ []\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int n = nums.size();\n        \n        // Calculate the prefix sum of the input array \'nums\'\n        vector<int> prefix = {nums[0]};\n        for (int i = 1; i < n; i++) {\n            prefix.push_back(prefix[i - 1] + nums[i]);\n        }\n        \n        vector<int> ans;\n        for (int i = 0; i < n; i++) {\n            // Calculate the sum of elements to the left and right of the current element\n            int leftSum = prefix[i] - nums[i];\n            int rightSum = prefix[n - 1] - prefix[i];\n            \n            // Calculate the count of elements to the left and right of the current element\n            int leftCount = i;\n            int rightCount = n - 1 - i;\n            \n            // Calculate the absolute difference sums for left and right parts\n            int leftTotal = leftCount * nums[i] - leftSum;\n            int rightTotal = rightSum - rightCount * nums[i];\n            \n            // Calculate the total sum of absolute differences for the current element\n            ans.push_back(leftTotal + rightTotal);\n        }\n        \n        return ans;\n    }\n};\n```\n```C []\nint* getSumAbsoluteDifferences(int* nums, int numsSize) {\n    int n = numsSize;\n    int* prefix = (int*)malloc(n * sizeof(int));\n    prefix[0] = nums[0];\n\n    // Calculate prefix sums\n    for (int i = 1; i < n; i++) {\n        prefix[i] = prefix[i - 1] + nums[i];\n    }\n\n    int* ans = (int*)malloc(n * sizeof(int));\n\n    for (int i = 0; i < n; i++) {\n        int leftSum = prefix[i] - nums[i];\n        int rightSum = prefix[n - 1] - prefix[i];\n        \n        int leftCount = i;\n        int rightCount = n - 1 - i;\n        \n        int leftTotal = leftCount * nums[i] - leftSum;\n        int rightTotal = rightSum - rightCount * nums[i];\n        \n        ans[i] = leftTotal + rightTotal;\n    }\n\n    return ans;\n}\n\n```\n```java []\nclass Solution {\n    public int[] getSumAbsoluteDifferences(int[] nums) {\n        int n = nums.length;\n        int[] prefix = new int[n];\n        prefix[0] = nums[0];\n        for (int i = 1; i < n; i++) {\n            prefix[i] = prefix[i - 1] + nums[i];\n        }\n        \n        int[] ans = new int[n];\n        for (int i = 0; i < n; i++) {\n            int leftSum = prefix[i] - nums[i];\n            int rightSum = prefix[n - 1] - prefix[i];\n            \n            int leftCount = i;\n            int rightCount = n - 1 - i;\n            \n            int leftTotal = leftCount * nums[i] - leftSum;\n            int rightTotal = rightSum - rightCount * nums[i];\n            \n            ans[i] = leftTotal + rightTotal;\n        }\n        \n        return ans;\n    }\n}\n```\n```python3 []\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        n = len(nums)\n        prefix = [nums[0]]\n        for i in range(1, n):\n            prefix.append(prefix[-1] + nums[i])\n        \n        ans = []\n        for i in range(len(nums)):\n            left_sum = prefix[i] - nums[i]\n            right_sum = prefix[-1] - prefix[i]\n            \n            left_count = i\n            right_count = n - 1 - i\n            \n            left_total = left_count * nums[i] - left_sum\n            right_total = right_sum - right_count * nums[i]\n\n            ans.append(left_total + right_total)\n        \n        return ans\n```\n```javascript []\nfunction getSumAbsoluteDifferences(nums) {\n    const n = nums.length;\n    const prefix = [nums[0]];\n\n    // Calculate prefix sums\n    for (let i = 1; i < n; i++) {\n        prefix.push(prefix[i - 1] + nums[i]);\n    }\n\n    const ans = [];\n\n    for (let i = 0; i < n; i++) {\n        const leftSum = prefix[i] - nums[i];\n        const rightSum = prefix[n - 1] - prefix[i];\n        \n        const leftCount = i;\n        const rightCount = n - 1 - i;\n        \n        const leftTotal = leftCount * nums[i] - leftSum;\n        const rightTotal = rightSum - rightCount * nums[i];\n        \n        ans.push(leftTotal + rightTotal);\n    }\n\n    return ans;\n}\n\n```\n\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Array', 'Math', 'C', 'Prefix Sum', 'C++', 'Java', 'Python3', 'JavaScript']