<?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>factor Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/factor/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/factor/</link>
	<description></description>
	<lastBuildDate>Sun, 24 Jan 2021 05:29:51 +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>factor Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/factor/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1735. Count Ways to Make Array With Product</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1735-count-ways-to-make-array-with-product/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1735-count-ways-to-make-array-with-product/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 05:29:33 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[factor]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[prime]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8017</guid>

					<description><![CDATA[<p>You are given a 2D integer array,&#160;queries. For each&#160;queries[i], where&#160;queries[i] = [ni, ki], find the number of different ways you can place positive integers into&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1735-count-ways-to-make-array-with-product/">花花酱 LeetCode 1735. Count Ways to Make Array With Product</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 2D integer array,&nbsp;<code>queries</code>. For each&nbsp;<code>queries[i]</code>, where&nbsp;<code>queries[i] = [n<sub>i</sub>, k<sub>i</sub>]</code>, find the number of different ways you can place positive integers into an array of size&nbsp;<code>n<sub>i</sub></code>&nbsp;such that the product of the integers is&nbsp;<code>k<sub>i</sub></code>. As the number of ways may be too large, the answer to the&nbsp;<code>i<sup>th</sup></code>&nbsp;query is the number of ways&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p>Return&nbsp;<em>an integer array&nbsp;</em><code>answer</code><em>&nbsp;where&nbsp;</em><code>answer.length == queries.length</code><em>, and&nbsp;</em><code>answer[i]</code><em>&nbsp;is the answer to the&nbsp;</em><code>i<sup>th</sup></code><em>&nbsp;query.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queries = [[2,6],[5,1],[73,660]]
<strong>Output:</strong> [4,1,50734910]
<strong>Explanation:</strong>&nbsp;Each query is independent.
[2,6]: There are 4 ways to fill an array of size 2 that multiply to 6: [1,6], [2,3], [3,2], [6,1].
[5,1]: There is 1 way to fill an array of size 5 that multiply to 1: [1,1,1,1,1].
[73,660]: There are 1050734917 ways to fill an array of size 73 that multiply to 660. 1050734917 modulo 10<sup>9</sup> + 7 = 50734910.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queries = [[1,1],[2,2],[3,3],[4,4],[5,5]]
<strong>Output:</strong> [1,2,3,10,5]
</pre>



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



<ul><li><code>1 &lt;= queries.length &lt;= 10<sup>4</sup></code></li><li><code>1 &lt;= n<sub>i</sub>, k<sub>i</sub>&nbsp;&lt;= 10<sup>4</sup></code></li></ul>



<h2><strong>Solution1: DP</strong></h2>



<p>let dp(n, k) be the ways to have product k of array size n.<br>dp(n, k) = sum(dp(n &#8211; 1, i)) where i is a factor of k and i != k.<br>base case:<br>dp(0, 1) = 1, dp(0, *) = 0<br>dp(i, 1) = C(n, i)<br>e.g. <br>dp(2, 6) = dp(1, 1) + dp(1, 2) + dp(1, 3) <br>= 2 + 1 + 1 = 4<br>dp(4, 4) = dp(3, 1) + dp(3, 2) <br>= dp(3, 1) + dp(2, 1)<br>= 4 + 6 = 10<br></p>



<p>Time complexity: O(sum(k_i))?<br>Space complexity: O(sum(k_i))?</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; waysToFillArray(vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    constexpr int kMod = 1e9 + 7;
    int n = 0;
    unordered_map&lt;int, int&gt; c, dp;
    function&lt;int(int, int)&gt; cnk = [&amp;](int n, int k) {
      if (k &gt; n) return 0;
      if (k == 0 || k == n) return 1;      
      int&amp; ans = c[(n &lt;&lt; 16) | k];
      if (!ans) ans = (cnk(n - 1, k - 1) + cnk(n - 1, k)) % kMod; 
      return ans;      
    };
    
    function&lt;int(int, int)&gt; dfs = [&amp;](int s, int k) -&gt; int {
      if (s == 0) return k == 1;
      if (k == 1) return cnk(n, s);
      int&amp; ans = dp[(s &lt;&lt; 16) | k];
      if (ans) return ans;      
      for (int i = 1; i * i &lt;= k; ++i) {
        if (k % i) continue;
        if (i != 1) 
          ans = (ans + dfs(s - 1, k / i)) % kMod;
        if (i * i != k)
          ans = (ans + dfs(s - 1, i)) % kMod;
      }
      return ans;
    };
    
    vector&lt;int&gt; ans;
    for (const auto&amp; q : queries) {
      dp.clear();
      n = q[0];
      ans.push_back(dfs(n, q[1]));
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1735-count-ways-to-make-array-with-product/">花花酱 LeetCode 1735. Count Ways to Make Array With Product</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/dynamic-programming/leetcode-1735-count-ways-to-make-array-with-product/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1627. Graph Connectivity With Threshold</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1627-graph-connectivity-with-threshold/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1627-graph-connectivity-with-threshold/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 18 Oct 2020 19:40:14 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[factor]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7534</guid>

					<description><![CDATA[<p>We have&#160;n&#160;cities labeled from&#160;1&#160;to&#160;n. Two different cities with labels&#160;x&#160;and&#160;y&#160;are directly connected by a bidirectional road if and only if&#160;x&#160;and&#160;y&#160;share a common divisor&#160;strictly greater&#160;than some&#160;threshold. More&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1627-graph-connectivity-with-threshold/">花花酱 LeetCode 1627. Graph Connectivity With Threshold</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>We have&nbsp;<code>n</code>&nbsp;cities labeled from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>n</code>. Two different cities with labels&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>&nbsp;are directly connected by a bidirectional road if and only if&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>&nbsp;share a common divisor&nbsp;<strong>strictly greater</strong>&nbsp;than some&nbsp;<code>threshold</code>. More formally, cities with labels&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>&nbsp;have a road between them if there exists an integer&nbsp;<code>z</code>&nbsp;such that all of the following are true:</p>



<ul><li><code>x % z == 0</code>,</li><li><code>y % z == 0</code>, and</li><li><code>z &gt; threshold</code>.</li></ul>



<p>Given the two integers,&nbsp;<code>n</code>&nbsp;and&nbsp;<code>threshold</code>, and an array of&nbsp;<code>queries</code>, you must determine for each&nbsp;<code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;if cities&nbsp;<code>a<sub>i</sub></code>&nbsp;and&nbsp;<code>b<sub>i</sub></code>&nbsp;are connected (i.e. there is some path between them).</p>



<p>Return&nbsp;<em>an array&nbsp;</em><code>answer</code><em>, where&nbsp;</em><code>answer.length == queries.length</code><em>&nbsp;and&nbsp;</em><code>answer[i]</code><em>&nbsp;is&nbsp;</em><code>true</code><em>&nbsp;if for the&nbsp;</em><code>i<sup>th</sup></code><em>&nbsp;query, there is a path between&nbsp;</em><code>a<sub>i</sub></code><em>&nbsp;and&nbsp;</em><code>b<sub>i</sub></code><em>, or&nbsp;</em><code>answer[i]</code><em>&nbsp;is&nbsp;</em><code>false</code><em>&nbsp;if there is no path.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/09/ex1.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]]
<strong>Output:</strong> [false,false,true]
<strong>Explanation:</strong> The divisors for each number:
1:   1
2:   1, 2
3:   1, 3
4:   1, 2, 4
5:   1, 5
6:   1, 2, 3, 6
Using the underlined divisors above the threshold, only cities 3 and 6 share a common divisor, so they are the
only ones directly connected. The result of each query:
[1,4]   1 is not connected to 4
[2,5]   2 is not connected to 5
[3,6]   3 is connected to 6 through path 3--6
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/10/tmp.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]]
<strong>Output:</strong> [true,true,true,true,true]
<strong>Explanation:</strong> The divisors for each number are the same as the previous example. However, since the threshold is 0,
all divisors can be used. Since all numbers share 1 as a divisor, all cities are connected.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/17/ex3.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]]
<strong>Output:</strong> [false,false,false,false,false]
<strong>Explanation:</strong> Only cities 2 and 4 share a common divisor 2 which is strictly greater than the threshold 1, so they are the only ones directly connected.
Please notice that there can be multiple queries for the same pair of nodes [x, y], and that the query [x, y] is equivalent to the query [y, x].
</pre>



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



<ul><li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li><li><code>0 &lt;= threshold &lt;= n</code></li><li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li><li><code>queries[i].length == 2</code></li><li><code>1 &lt;= a<sub>i</sub>, b<sub>i</sub>&nbsp;&lt;= cities</code></li><li><code>a<sub>i</sub>&nbsp;!= b<sub>i</sub></code></li></ul>



<h2><strong>Solution: Union Find</strong></h2>



<p>For x, merge 2x, 3x, 4x, ..,<br>If a number is already &#8220;merged&#8221;, skip it.</p>



<p>Time complexity: O(nlogn? + queries)? <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;bool&gt; areConnected(int n, int threshold, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    if (threshold == 0) return vector&lt;bool&gt;(queries.size(), true);
    
    vector&lt;int&gt; ds(n + 1);
    iota(begin(ds), end(ds), 0);
    function&lt;int(int)&gt; find = [&amp;](int x) {
      return ds[x] == x ? x : ds[x] = find(ds[x]);
    };
    
    for (int x = threshold + 1; x &lt;= n; ++x)
      if (ds[x] == x)
        for (int y = 2 * x; y &lt;= n; y += x)    
          ds[max(find(x), find(y))] = min(find(x), find(y));
    
    vector&lt;bool&gt; ans;
    for (const vector&lt;int&gt;&amp; q : queries)
      ans.push_back(find(q[0]) == find(q[1]));    
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def areConnected(self, n: int, threshold: int, 
                   queries: List[List[int]]) -&gt; List[bool]:
    if threshold == 0: return [True] * len(queries)
    
    ds = list(range(n + 1))
    def find(x: int) -&gt; int:
      if x != ds[x]: ds[x] = find(ds[x])
      return ds[x]

    for x in range(threshold + 1, n + 1):
      if ds[x] == x:
        for y in range(2 * x, n + 1, x):
          ds[max(find(x), find(y))] = min(find(x), find(y))

    return [find(x) == find(y) for x, y in queries]</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1627-graph-connectivity-with-threshold/">花花酱 LeetCode 1627. Graph Connectivity With Threshold</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/graph/leetcode-1627-graph-connectivity-with-threshold/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1492. The kth Factor of n</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1492-the-kth-factor-of-n/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1492-the-kth-factor-of-n/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 27 Jun 2020 18:14:42 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[factor]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6972</guid>

					<description><![CDATA[<p>Given two positive integers&#160;n&#160;and&#160;k. A factor of an integer&#160;n&#160;is defined as an integer&#160;i&#160;where&#160;n % i == 0. Consider a list of all factors of&#160;n&#160;sorted in&#160;ascending&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1492-the-kth-factor-of-n/">花花酱 LeetCode 1492. The kth Factor of n</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 two positive integers&nbsp;<code>n</code>&nbsp;and&nbsp;<code>k</code>.</p>



<p>A factor of an integer&nbsp;<code>n</code>&nbsp;is defined as an integer&nbsp;<code>i</code>&nbsp;where&nbsp;<code>n % i == 0</code>.</p>



<p>Consider a list of all factors of&nbsp;<code>n</code>&nbsp;sorted in&nbsp;<strong>ascending order</strong>, return&nbsp;<em>the&nbsp;</em><code>kth</code><em>&nbsp;factor</em>&nbsp;in this list or return&nbsp;<strong>-1</strong>&nbsp;if&nbsp;<code>n</code>&nbsp;has less than&nbsp;<code>k</code>&nbsp;factors.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 12, k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 7, k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> Factors list is [1, 7], the 2nd factor is 7.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, k = 4
<strong>Output:</strong> -1
<strong>Explanation:</strong> Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1, k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> Factors list is [1], the 1st factor is 1.
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1000, k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000].
</pre>



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



<ul><li><code>1 &lt;= k &lt;= n &lt;= 1000</code></li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<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 kthFactor(int n, int k) {
    for (int i = 1; i &lt;= n; ++i)
      if (n % i == 0 &amp;&amp; --k == 0) return i;
    return -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1492-the-kth-factor-of-n/">花花酱 LeetCode 1492. The kth Factor of n</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-1492-the-kth-factor-of-n/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 952. Largest Component Size by Common Factor</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-952-largest-component-size-by-common-factor/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-952-largest-component-size-by-common-factor/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 02 Dec 2018 07:14:51 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[factor]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4392</guid>

					<description><![CDATA[<p>Problem Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-952-largest-component-size-by-common-factor/">花花酱 LeetCode 952. Largest Component Size by Common Factor</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><iframe width="500" height="375" src="https://www.youtube.com/embed/GTX0kw63Tn0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given a non-empty array of unique positive integers <code>A</code>, consider the following graph:</p>
<ul>
<li>There are <code>A.length</code> nodes, labelled <code>A[0]</code> to <code>A[A.length - 1];</code></li>
<li>There is an edge between <code>A[i]</code> and <code>A[j]</code> if and only if <code>A[i]</code> and <code>A[j]</code> share a common factor greater than 1.</li>
</ul>
<p>Return the size of the largest connected component in the graph.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[4,6,15,35]</span>
<strong>Output: </strong><span id="example-output-1">4</span>
<img src="https://assets.leetcode.com/uploads/2018/12/01/ex1.png" alt="" />
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[20,50,9,63]</span>
<strong>Output: </strong><span id="example-output-2">2</span>
<img src="https://assets.leetcode.com/uploads/2018/12/01/ex2.png" alt="" />
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">[2,3,6,7,4,12,21,39]</span>
<strong>Output: </strong><span id="example-output-3">8</span>
<img src="https://assets.leetcode.com/uploads/2018/12/01/ex3.png" alt="" />
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= A.length &lt;= 20000</code></li>
<li><code>1 &lt;= A[i] &lt;= 100000</code></li>
</ol>
<h1><strong>Solution: Union Find</strong></h1>
<p><img class="alignnone size-full wp-image-4400" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/952-ep232.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/952-ep232.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/952-ep232-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/12/952-ep232-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p>For each number, union itself with all its factors.</p>
<p>E.g. 6, union(6,2), union(6,3)</p>
<p>Time complexity: \( O(\Sigma{sqrt(A[i])})  \)</p>
<p>Space complexity: \( O(max(A)) \)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, running time: 148 ms
class DSU {
public:
  DSU(int n): p_(n) {
    for (int i = 0; i &lt; n; ++i)
      p_[i] = i;
  }
  
  void Union(int x, int y) {
    p_[Find(x)] = p_[Find(y)];
  }
  
  int Find(int x) {
    if (p_[x] != x) p_[x] = Find(p_[x]);
    return p_[x];
  }
private:
  vector&lt;int&gt; p_;
};

class Solution {
public:
  int largestComponentSize(vector&lt;int&gt;&amp; A) {    
    int n = *max_element(begin(A), end(A));
    DSU dsu(n + 1);
    for (int a : A) {
      int t = sqrt(a);
      for (int k = 2; k &lt;= t; ++k)
        if (a % k == 0) {
          dsu.Union(a, k);
          dsu.Union(a, a / k);
        }
    }
    unordered_map&lt;int, int&gt; c;
    int ans = 1;
    for (int a : A)
      ans = max(ans, ++c[dsu.Find(a)]);    
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua, running time: 3432 ms
class Solution:
  def largestComponentSize(self, A):
    p = list(range(max(A) + 1))
       
    def find(x):
      while p[x] != x:
        p[x] = p[p[x]]
        x = p[x]
      return x
    
    def union(x, y):
      p[find(x)] = p[find(y)]      
      
    for a in A:     
      for k in range(2, int(math.sqrt(a) + 1)):        
        if a % k == 0:
          union(a, k)
          union(a, a // k)
    
    return collections.Counter([find(a) for a in A]).most_common(1)[0][1]</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-952-largest-component-size-by-common-factor/">花花酱 LeetCode 952. Largest Component Size by Common Factor</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/graph/leetcode-952-largest-component-size-by-common-factor/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
