<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pre-compute Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pre-compute-2/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pre-compute-2/</link>
	<description></description>
	<lastBuildDate>Sat, 01 Jan 2022 00:26:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>pre-compute Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pre-compute-2/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1991. Find the Middle Index in Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1991-find-the-middle-index-in-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1991-find-the-middle-index-in-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 01 Jan 2022 00:26:32 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[pre-compute]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9372</guid>

					<description><![CDATA[<p>Given a&#160;0-indexed&#160;integer array&#160;nums, find the&#160;leftmost&#160;middleIndex&#160;(i.e., the smallest amongst all the possible ones). A&#160;middleIndex&#160;is an index where&#160;nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] +&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1991-find-the-middle-index-in-array/">花花酱 LeetCode 1991. Find the Middle Index in Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<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>



<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>



<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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,-1,8,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
The sum of the numbers after index 3 is: 4 = 4
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,-1,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The sum of the numbers before index 2 is: 1 + -1 = 0
The sum of the numbers after index 2 is: 0
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,5]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no valid middleIndex.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li></ul>



<h2><strong>Solution: Pre-compute + prefix sum</strong></h2>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int findMiddleIndex(vector&lt;int&gt;&amp; nums) {
    int sum = accumulate(begin(nums), end(nums), 0);
    for (int i = 0, cur = 0; i &lt; nums.size(); ++i) {
      if (cur == sum - cur - nums[i]) return i;
      cur += nums[i];
    }
    return -1;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1991-find-the-middle-index-in-array/">花花酱 LeetCode 1991. Find the Middle Index in Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1991-find-the-middle-index-in-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
