<?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>differential array Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/differential-array/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/differential-array/</link>
	<description></description>
	<lastBuildDate>Wed, 02 Dec 2020 07:29:21 +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>differential array Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/differential-array/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1674. Minimum Moves to Make Array Complementary</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1674-minimum-moves-to-make-array-complementary/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1674-minimum-moves-to-make-array-complementary/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Nov 2020 19:22:24 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[differential array]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7740</guid>

					<description><![CDATA[<p>You are given an integer array&#160;nums&#160;of&#160;even&#160;length&#160;n&#160;and an integer&#160;limit. In one move, you can replace any integer from&#160;nums&#160;with another integer between&#160;1&#160;and&#160;limit, inclusive. The array&#160;nums&#160;is&#160;complementary&#160;if for all&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1674-minimum-moves-to-make-array-complementary/">花花酱 LeetCode 1674. Minimum Moves to Make Array Complementary</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1674. Minimum Moves to Make Array Complementary - 刷题找工作 EP373" width="500" height="281" src="https://www.youtube.com/embed/HvMhvPKjCSU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an integer array&nbsp;<code>nums</code>&nbsp;of&nbsp;<strong>even</strong>&nbsp;length&nbsp;<code>n</code>&nbsp;and an integer&nbsp;<code>limit</code>. In one move, you can replace any integer from&nbsp;<code>nums</code>&nbsp;with another integer between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>limit</code>, inclusive.</p>



<p>The array&nbsp;<code>nums</code>&nbsp;is&nbsp;<strong>complementary</strong>&nbsp;if for all indices&nbsp;<code>i</code>&nbsp;(<strong>0-indexed</strong>),&nbsp;<code>nums[i] + nums[n - 1 - i]</code>&nbsp;equals the same number. For example, the array&nbsp;<code>[1,2,3,4]</code>&nbsp;is complementary because for all indices&nbsp;<code>i</code>,&nbsp;<code>nums[i] + nums[n - 1 - i] = 5</code>.</p>



<p>Return the&nbsp;<em><strong>minimum</strong>&nbsp;number of moves required to make&nbsp;</em><code>nums</code><em>&nbsp;<strong>complementary</strong></em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,4,3], limit = 4
<strong>Output:</strong> 1
<strong>Explanation:</strong> In 1 move, you can change nums to [1,2,2,3] (underlined elements are changed).
nums[0] + nums[3] = 1 + 3 = 4.
nums[1] + nums[2] = 2 + 2 = 4.
nums[2] + nums[1] = 2 + 2 = 4.
nums[3] + nums[0] = 3 + 1 = 4.
Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,2,1], limit = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 &gt; limit.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,1,2], limit = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already complementary.
</pre>



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



<ul><li><code>n == nums.length</code></li><li><code>2 &lt;= n&nbsp;&lt;=&nbsp;10<sup>5</sup></code></li><li><code>1 &lt;= nums[i]&nbsp;&lt;= limit &lt;=&nbsp;10<sup>5</sup></code></li><li><code>n</code>&nbsp;is even.</li></ul>



<h2><strong>Solution: Sweep Line / Prefix Sum</strong></h2>



<p>Let a = min(nums[i], nums[n-i-1]), b = max(nums[i], nums[n-i-1])<br>The key to this problem is how many moves do we need to make a + b == T.</p>



<p>if 2 &lt;= T &lt; a + 1, two moves, lower both a and b.<br>if a +1 &lt;= T &lt; a + b, one move, lower b<br>if a + b == T, zero move<br>if a + b + 1 &lt;= T &lt; b + limit + 1, one move, increase a<br>if b + limit + 1 &lt;= T &lt;= 2*limit, two moves, increase both a and b.</p>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-1.png" alt="" class="wp-image-7759" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-2.png" alt="" class="wp-image-7760" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-3.png" alt="" class="wp-image-7761" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1674-ep373-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Time complexity: O(n + limit) or O(nlogn) if limit &gt;&gt;n<br>Space complexity: O(limit) or O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minMoves(vector&lt;int&gt;&amp; nums, int limit) {
    const int n = nums.size();
    vector&lt;int&gt; delta(limit * 2 + 2);
    for (int i = 0; i &lt; n / 2; ++i) {
      const int a = min(nums[i], nums[n - i - 1]);
      const int b = max(nums[i], nums[n - i - 1]);
      delta[2] += 2;          // dec a, dec b
      --delta[a + 1];         // dec a 
      --delta[a + b];         // no op
      ++delta[a + b + 1];     // inc a
      ++delta[b + limit + 1]; // inc a, inc b     
    }
    int ans = n;
    for (int t = 2, cur = 0; t &lt;= limit * 2; ++t) {
      cur += delta[t];
      ans = min(ans, cur);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1674-minimum-moves-to-make-array-complementary/">花花酱 LeetCode 1674. Minimum Moves to Make Array Complementary</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/hashtable/leetcode-1674-minimum-moves-to-make-array-complementary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
