{"id":7790,"date":"2020-12-12T16:48:31","date_gmt":"2020-12-13T00:48:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7790"},"modified":"2020-12-12T17:12:27","modified_gmt":"2020-12-13T01:12:27","slug":"leetcode-1685-sum-of-absolute-differences-in-a-sorted-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1685-sum-of-absolute-differences-in-a-sorted-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1685. Sum of Absolute Differences in a Sorted Array"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;sorted in&nbsp;<strong>non-decreasing<\/strong>&nbsp;order.<\/p>\n\n\n\n<p>Build and return&nbsp;<em>an integer array&nbsp;<\/em><code>result<\/code><em>&nbsp;with the same length as&nbsp;<\/em><code>nums<\/code><em>&nbsp;such that&nbsp;<\/em><code>result[i]<\/code><em>&nbsp;is equal to the&nbsp;<strong>summation of absolute differences<\/strong>&nbsp;between&nbsp;<\/em><code>nums[i]<\/code><em>&nbsp;and all the other elements in the array.<\/em><\/p>\n\n\n\n<p>In other words,&nbsp;<code>result[i]<\/code>&nbsp;is equal to&nbsp;<code>sum(|nums[i]-nums[j]|)<\/code>&nbsp;where&nbsp;<code>0 &lt;= j &lt; nums.length<\/code>&nbsp;and&nbsp;<code>j != i<\/code>&nbsp;(<strong>0-indexed<\/strong>).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,3,5]\n<strong>Output:<\/strong> [4,3,5]\n<strong>Explanation:<\/strong> Assuming the arrays are 0-indexed, then\nresult[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,\nresult[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,\nresult[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,4,6,8,10]\n<strong>Output:<\/strong> [24,15,13,15,21]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= nums[i + 1] &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum<\/strong><\/h2>\n\n\n\n<p>Let s[i] denote sum(num[i] &#8211; num[j]) 0 &lt;= j &lt;= i<br>s[i] = s[i &#8211; 1] + (num[i] &#8211; num[i &#8211; 1]) * i<br>Let l[i] denote sum(nums[j] &#8211; nums[i]) i &lt;= j &lt; n<br>l[i] = l[i + 1] + (nums[i + 1] &#8211; num[i]) * (n &#8211; i &#8211; 1)<br>ans[i] = s[i] + l[i]<\/p>\n\n\n\n<p>e.g. 1, 3, 7, 9<br>s[0] = 0<br>s[1] = 0 + (3 &#8211; 1) * 1 = 2<br>s[2] = 2 + (7 &#8211; 3) * 2 = 10<br>s[3] = 10 + (9 &#8211; 7) * 3 = 16<br>l[3] = 0<br>l[2] = 0 + (9 &#8211; 7) * 1 = 2<br>l[1] = 2 + (7 &#8211; 3) * 2 = 10<br>l[0] = 10 + (3 &#8211; 1) *  3 = 16<\/p>\n\n\n\n<p>ans = [16, 12, 12, 16]<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n    const int n = nums.size();\n    vector<int> ans(n);    \n    for (int i = 1, sum = 0; i < n; ++i)\n      ans[i] += (sum += (nums[i] - nums[i - 1]) * i);\n    for (int i = n - 2, sum = 0; i >= 0; --i)\n      ans[i] += (sum += (nums[i + 1] - nums[i]) * (n - i - 1));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;sorted in&nbsp;non-decreasing&nbsp;order. Build and return&nbsp;an integer array&nbsp;result&nbsp;with the same length as&nbsp;nums&nbsp;such that&nbsp;result[i]&nbsp;is equal to the&nbsp;summation of absolute differences&nbsp;between&nbsp;nums[i]&nbsp;and all the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,376,200],"class_list":["post-7790","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-on","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7790"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7790\/revisions"}],"predecessor-version":[{"id":7792,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7790\/revisions\/7792"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}