{"id":9372,"date":"2021-12-31T16:26:32","date_gmt":"2022-01-01T00:26:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9372"},"modified":"2021-12-31T16:26:49","modified_gmt":"2022-01-01T00:26:49","slug":"leetcode-1991-find-the-middle-index-in-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1991-find-the-middle-index-in-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1991. Find the Middle Index in Array"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, find the&nbsp;<strong>leftmost<\/strong>&nbsp;<code>middleIndex<\/code>&nbsp;(i.e., the smallest amongst all the possible ones).<\/p>\n\n\n\n<p>A&nbsp;<code>middleIndex<\/code>&nbsp;is an index where&nbsp;<code>nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1]<\/code>.<\/p>\n\n\n\n<p>If&nbsp;<code>middleIndex == 0<\/code>, the left side sum is considered to be&nbsp;<code>0<\/code>. Similarly, if&nbsp;<code>middleIndex == nums.length - 1<\/code>, the right side sum is considered to be&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>leftmost<\/strong>&nbsp;<\/em><code>middleIndex<\/code><em>&nbsp;that satisfies the condition, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if there is no such index<\/em>.<\/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,-1,8,4]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The sum of the numbers before index 3 is: 2 + 3 + -1 = 4\nThe sum of the numbers after index 3 is: 4 = 4\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,-1,4]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The sum of the numbers before index 2 is: 1 + -1 = 0\nThe sum of the numbers after index 2 is: 0\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,5]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There is no valid middleIndex.\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;= 100<\/code><\/li><li><code>-1000 &lt;= nums[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Pre-compute + prefix sum<\/strong><\/h2>\n\n\n\n<p>Pre-compute the sum of entire array. We scan the array and accumulate prefix sum and we can compute the sum of the rest of array.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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  int findMiddleIndex(vector<int>& nums) {\n    int sum = accumulate(begin(nums), end(nums), 0);\n    for (int i = 0, cur = 0; i < nums.size(); ++i) {\n      if (cur == sum - cur - nums[i]) return i;\n      cur += nums[i];\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, find the&nbsp;leftmost&nbsp;middleIndex&nbsp;(i.e., the smallest amongst all the possible ones). A&nbsp;middleIndex&nbsp;is an index where&nbsp;nums[0] + nums[1] + &#8230; + nums[middleIndex-1] == nums[middleIndex+1] +&#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,761],"class_list":["post-9372","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-pre-compute-2","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9372","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=9372"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9372\/revisions"}],"predecessor-version":[{"id":9374,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9372\/revisions\/9374"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}