Using prefix and suffix array, Easy javascript code
you can check out my github repository where i am uploading famous interview questions topic wise with solutions.\nlink-- https://github.com/Abhaydutt2003/DataS
you can check out my github repository where i am uploading famous interview questions topic wise with solutions.\nlink-- https://github.com/Abhaydutt2003/DataStructureAndAlgoPractice \nkindly upvote if you like my solution. you can ask doubts below.\xAF\n    \n\n# Code\n```\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar getSumAbsoluteDifferences = function(nums) {\n    //build prefix array\n  let pre = new Array(nums.length);\n  for (let i = 0; i < nums.length; i++) {\n    if (i == 0) {\n      pre[i] = nums[i];\n    } else {\n      pre[i] = nums[i] + pre[i - 1];\n    }\n  }\n  //build suffix array\n  let suff = new Array(nums.length);\n  for (let i = nums.length - 1; i >= 0; i--) {\n    if (i == nums.length - 1) {\n      suff[i] = nums[i];\n    } else {\n      suff[i] = nums[i] + suff[i + 1];\n    }\n  }\n  //now calculate the answer\n  let ans = [];\n  for (let i = 0; i < nums.length; i++) {\n    let right = suff[i] - nums[i] * (nums.length - i);\n    let left = nums[i] * (i + 1) - pre[i];\n    ans.push(left + right);\n  }\n  return ans;\n};\n```
['Math', 'Prefix Sum', 'JavaScript']