[ C++ ] Explain
To Understand my code Let see One Example:\n\nfor array [1,4,6,8,10]\nfirst calculate the Sum of Absolute Differences for first Index which is 24 here\nThen as 
To Understand my code Let see One Example:\n```\nfor array [1,4,6,8,10]\nfirst calculate the Sum of Absolute Differences for first Index which is 24 here\nThen as array is sorted Use it for finding the next index Sum of Absolute Differences by Using fallowing Formula\n\ni\'th index\'s Sum of Absolute Differences = Sum of Absolute Differences [ i-1 ] + i*(arr[i]-arr[i-1]) - (arr[i]-arr[i-1]) * (total_no_ele - i)\n\n\n\nfor Index 1  element = 4  differnce from previous element is 4-1 = 3 ,and as It is a 2nd element from that array\nso Sum of Absolute Differences for Index 1 is = > 24 + 3*idex - 3*(n-index) = 24 + 3*1 - 3*(5-1) = 15\n    \nfor Index 2  element = 6  differnce from previous element is 6-4 = 2 ,and as It is a 3rd element from that array\nso Sum of Absolute Differences for Index 2 is = > 15 + 2*index - 2*(n-index) = 15 + 2*2 - 2*(5-2) = 13\n\nfor Index 3  element = 8  differnce from previous element is 8-6 = 2 ,and as It is a 4rth element from that array\nso Sum of Absolute Differences for Index 3 is = > 13 + 2*index - 2*(n-index) = 13 + 2*3 - 2*(5-3) = 15\n    \nfor Index 4  element = 10  differnce from previous element is 10-8 = 2 ,and as It is a 5th element from that array\nso Sum of Absolute Differences for Index 4 is = > 15 + 2*index - 2*(n-index) = 15 + 2*4 - 2*(5-4) = 21\n    \nnow return [24,15,13,15,21]\n```\n**For Those Who want to Know How I came up to This Approch Please see Fallowing**\n```\nOne thing keep in mind is that it is a sorted array\nif array is [ 1 4 6 8 10 ]\nfor element 6 diff are 5 ,2, 0 , 2, 4 now\nfor 8 diffrs are going to be 5+(8-6) , 2+(8-6) , 0+(8-6) , 2+(6-8) , 4+(6 - 8) => 7 4 2 0 2 = 15\nsame way for 10 diffrs  are going to be  7+(10-8) , 4+(10-8) , 2+(10-8) , 0+(10-8) , 2+(8 - 10) = > 9 6 4 2 0\nHope it clear now\n```\n\n**Time Complexcity O( N )\nSpace Complexcity O( 1 )** <- except for that answer array\n\n```\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        vector<int>ans(nums.size(),0);\n        for(int i = 1;i<nums.size();i++)\n            ans[0]+=(nums[i]-nums[0]);\n        \n        for(int j = 1;j<nums.size();j++)\n            ans[j] = ans[j-1]+(nums[j]-nums[j-1])*j-(nums[j]-nums[j-1])*(nums.size()-j);\n        \n        return ans;\n    }\n};\n```\n\n**Hope It\'s help you**\n**Please Upvote Then**\n\n**thank You**
[]