<?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>rearrange Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/rearrange/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/rearrange/</link>
	<description></description>
	<lastBuildDate>Sat, 05 Feb 2022 01:06:04 +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>rearrange Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/rearrange/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2149. Rearrange Array Elements by Sign</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-2149-rearrange-array-elements-by-sign/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-2149-rearrange-array-elements-by-sign/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Feb 2022 01:01:40 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rearrange]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9470</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;integer array&#160;nums&#160;of&#160;even&#160;length consisting of an&#160;equal&#160;number of positive and negative integers. You should&#160;rearrange&#160;the elements of&#160;nums&#160;such that the modified array follows the given conditions:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-2149-rearrange-array-elements-by-sign/">花花酱 LeetCode 2149. Rearrange Array Elements by Sign</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>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;of&nbsp;<strong>even</strong>&nbsp;length consisting of an&nbsp;<strong>equal</strong>&nbsp;number of positive and negative integers.</p>



<p>You should&nbsp;<strong>rearrange</strong>&nbsp;the elements of&nbsp;<code>nums</code>&nbsp;such that the modified array follows the given conditions:</p>



<ol><li>Every&nbsp;<strong>consecutive pair</strong>&nbsp;of integers have&nbsp;<strong>opposite signs</strong>.</li><li>For all integers with the same sign, the&nbsp;<strong>order</strong>&nbsp;in which they were present in&nbsp;<code>nums</code>&nbsp;is&nbsp;<strong>preserved</strong>.</li><li>The rearranged array begins with a positive integer.</li></ol>



<p>Return&nbsp;<em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,1,-2,-5,2,-4]
<strong>Output:</strong> [3,-2,1,-5,2,-4]
<strong>Explanation:</strong>
The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.  
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,1]
<strong>Output:</strong> [1,-1]
<strong>Explanation:</strong>
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to [1,-1].
</pre>



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



<ul><li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li><li><code>nums.length</code>&nbsp;is&nbsp;<strong>even</strong></li><li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li><li><code>nums</code>&nbsp;consists of&nbsp;<strong>equal</strong>&nbsp;number of positive and negative integers.</li></ul>



<h2><strong>Solution 1: Split and merge</strong></h2>



<p>Create two arrays to store positive and negative numbers.</p>



<p>Time complexity: O(n)<br>Space complexity: 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:
  vector&lt;int&gt; rearrangeArray(vector&lt;int&gt;&amp; nums) {
    vector&lt;int&gt; pos;
    vector&lt;int&gt; neg;
    for (int x : nums)
      (x &gt; 0 ? pos : neg).push_back(x);
    vector&lt;int&gt; ans;
    for (int i = 0; i &lt; pos.size(); ++i) {
      ans.push_back(pos[i]);
      ans.push_back(neg[i]);
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Two Pointers</strong></h2>



<p>Use two pointers to store the next pos / neg.</p>



<p>Time complexity: O(n)<br>Space complexity: 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:
  vector&lt;int&gt; rearrangeArray(vector&lt;int&gt;&amp; nums) {
    vector&lt;int&gt; ans(nums.size());    
    int pos = 0;
    int neg = 1;
    for (int x : nums) {
      int&amp; index = x &gt; 0 ? pos : neg;
      ans[index] = x;
      index += 2;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-2149-rearrange-array-elements-by-sign/">花花酱 LeetCode 2149. Rearrange Array Elements by Sign</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/two-pointers/leetcode-2149-rearrange-array-elements-by-sign/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
