{"id":9964,"date":"2023-02-25T21:54:12","date_gmt":"2023-02-26T05:54:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9964"},"modified":"2023-02-25T21:54:54","modified_gmt":"2023-02-26T05:54:54","slug":"leetcode-2574-left-and-right-sum-differences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2574-left-and-right-sum-differences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2574.\u00a0Left and Right Sum Differences"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, find a&nbsp;<strong>0-indexed&nbsp;<\/strong>integer array&nbsp;<code>answer<\/code>&nbsp;where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>answer.length == nums.length<\/code>.<\/li><li><code>answer[i] = |leftSum[i] - rightSum[i]|<\/code>.<\/li><\/ul>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>leftSum[i]<\/code>&nbsp;is the sum of elements to the left of the index&nbsp;<code>i<\/code>&nbsp;in the array&nbsp;<code>nums<\/code>. If there is no such element,&nbsp;<code>leftSum[i] = 0<\/code>.<\/li><li><code>rightSum[i]<\/code>&nbsp;is the sum of elements to the right of the index&nbsp;<code>i<\/code>&nbsp;in the array&nbsp;<code>nums<\/code>. If there is no such element,&nbsp;<code>rightSum[i] = 0<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the array<\/em>&nbsp;<code>answer<\/code>.<\/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 = [10,4,8,3]\n<strong>Output:<\/strong> [15,1,11,22]\n<strong>Explanation:<\/strong> The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].\nThe array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].\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]\n<strong>Output:<\/strong> [0]\n<strong>Explanation:<\/strong> The array leftSum is [0] and the array rightSum is [0].\nThe array answer is [|0 - 0|] = [0].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: O(1) Space<\/strong><\/p>\n\n\n\n<p>Pre-compute the sum of all numbers as right sum, and accumulate left sum on the fly then we can achieve O(1) space.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<p><\/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> leftRigthDifference(vector<int>& nums) {\n    int right = accumulate(begin(nums), end(nums), 0);\n    vector<int> ans;\n    for (int i = 0, left = 0; i < nums.size(); ++i) {\n      right -= nums[i];\n      ans.push_back(abs(left - right));\n      left += nums[i];      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, find a&nbsp;0-indexed&nbsp;integer array&nbsp;answer&nbsp;where: answer.length == nums.length. answer[i] = |leftSum[i] &#8211; rightSum[i]|. Where: leftSum[i]&nbsp;is the sum of elements to the left of 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":[184],"tags":[20,222,200],"class_list":["post-9964","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9964","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=9964"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9964\/revisions"}],"predecessor-version":[{"id":9966,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9964\/revisions\/9966"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}