<?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>co-prime Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/co-prime/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/co-prime/</link>
	<description></description>
	<lastBuildDate>Tue, 08 Mar 2022 12:19:55 +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>co-prime Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/co-prime/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2197. Replace Non-Coprime Numbers in Array</title>
		<link>https://zxi.mytechroad.com/blog/stack/leetcode-2197-replace-non-coprime-numbers-in-array/</link>
					<comments>https://zxi.mytechroad.com/blog/stack/leetcode-2197-replace-non-coprime-numbers-in-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 08 Mar 2022 12:18:45 +0000</pubDate>
				<category><![CDATA[Stack]]></category>
		<category><![CDATA[co-prime]]></category>
		<category><![CDATA[gcd]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[lcm]]></category>
		<category><![CDATA[stack]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9561</guid>

					<description><![CDATA[<p>You are given an array of integers&#160;nums. Perform the following steps: Find&#160;any&#160;two&#160;adjacent&#160;numbers in&#160;nums&#160;that are&#160;non-coprime. If no such numbers are found,&#160;stop&#160;the process. Otherwise, delete the two&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-2197-replace-non-coprime-numbers-in-array/">花花酱 LeetCode 2197. Replace Non-Coprime Numbers 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>You are given an array of integers&nbsp;<code>nums</code>. Perform the following steps:</p>



<ol><li>Find&nbsp;<strong>any</strong>&nbsp;two&nbsp;<strong>adjacent</strong>&nbsp;numbers in&nbsp;<code>nums</code>&nbsp;that are&nbsp;<strong>non-coprime</strong>.</li><li>If no such numbers are found,&nbsp;<strong>stop</strong>&nbsp;the process.</li><li>Otherwise, delete the two numbers and&nbsp;<strong>replace</strong>&nbsp;them with their&nbsp;<strong>LCM (Least Common Multiple)</strong>.</li><li><strong>Repeat</strong>&nbsp;this process as long as you keep finding two adjacent non-coprime numbers.</li></ol>



<p>Return&nbsp;<em>the&nbsp;<strong>final</strong>&nbsp;modified array.</em>&nbsp;It can be shown that replacing adjacent non-coprime numbers in&nbsp;<strong>any</strong>&nbsp;arbitrary order will lead to the same result.</p>



<p>The test cases are generated such that the values in the final array are&nbsp;<strong>less than or equal</strong>&nbsp;to&nbsp;<code>10<sup>8</sup></code>.</p>



<p>Two values&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>&nbsp;are&nbsp;<strong>non-coprime</strong>&nbsp;if&nbsp;<code>GCD(x, y) &gt; 1</code>&nbsp;where&nbsp;<code>GCD(x, y)</code>&nbsp;is the&nbsp;<strong>Greatest Common Divisor</strong>&nbsp;of&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [6,4,3,2,7,6,2]
<strong>Output:</strong> [12,7,6]
<strong>Explanation:</strong> 
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong><u>12</u></strong>,3,2,7,6,2].
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong><u>12</u></strong>,2,7,6,2].
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong><u>12</u></strong>,7,6,2].
- (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,<strong>6</strong>].
There are no more adjacent non-coprime numbers in nums.
Thus, the final modified array is [12,7,6].
Note that there are other ways to obtain the same resultant array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,2,1,1,3,3,3]
<strong>Output:</strong> [2,1,1,3]
<strong>Explanation:</strong> 
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<strong>3</strong>,3].
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<strong>3</strong>].
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<strong>2</strong>,1,1,3].
There are no more adjacent non-coprime numbers in nums.
Thus, the final modified array is [2,1,1,3].
Note that there are other ways to obtain the same resultant array.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li><li>The test cases are generated such that the values in the final array are&nbsp;<strong>less than or equal</strong>&nbsp;to&nbsp;<code>10<sup>8</sup></code>.</li></ul>



<h2><strong>Solution: Stack</strong></h2>



<p>&#8220;&#8221;&#8221;It can be shown that replacing adjacent non-coprime numbers in&nbsp;<strong>any</strong>&nbsp;arbitrary order will lead to the same result.&#8221;&#8221;&#8221;</p>



<p>So that we can do it in one pass from left to right using a stack/vector.</p>



<p>Push the current number onto stack, and merge top two if they are not co-prime.</p>



<p>Time complexity: O(nlogm)<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; replaceNonCoprimes(vector&lt;int&gt;&amp; nums) {
    vector&lt;int&gt; ans;
    for (int x : nums) {
      ans.push_back(x);
      while (ans.size() &gt; 1) {
        const int n1 = ans[ans.size() - 1]; 
        const int n2 = ans[ans.size() - 2]; 
        const int d = gcd(n1, n2);
        if (d == 1) break;
        ans.pop_back();
        ans.pop_back();
        ans.push_back(n1 / d * n2);
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-2197-replace-non-coprime-numbers-in-array/">花花酱 LeetCode 2197. Replace Non-Coprime Numbers 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/stack/leetcode-2197-replace-non-coprime-numbers-in-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
