<?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>divisor Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/divisor/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/divisor/</link>
	<description></description>
	<lastBuildDate>Sat, 29 Apr 2023 01:37:31 +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>divisor Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/divisor/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2644. Find the Maximum Divisibility Score</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 01:36:56 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[divisor]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10003</guid>

					<description><![CDATA[<p>You are given two&#160;0-indexed&#160;integer arrays&#160;nums&#160;and&#160;divisors. The&#160;divisibility score&#160;of&#160;divisors[i]&#160;is the number of indices&#160;j&#160;such that&#160;nums[j]&#160;is divisible by&#160;divisors[i]. Return&#160;the integer&#160;divisors[i]&#160;with the maximum divisibility score. If there is more than&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/">花花酱 LeetCode 2644. Find the Maximum Divisibility Score</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 two&nbsp;<strong>0-indexed</strong>&nbsp;integer arrays&nbsp;<code>nums</code>&nbsp;and&nbsp;<code>divisors</code>.</p>



<p>The&nbsp;<strong>divisibility score</strong>&nbsp;of&nbsp;<code>divisors[i]</code>&nbsp;is the number of indices&nbsp;<code>j</code>&nbsp;such that&nbsp;<code>nums[j]</code>&nbsp;is divisible by&nbsp;<code>divisors[i]</code>.</p>



<p>Return&nbsp;<em>the integer</em>&nbsp;<code>divisors[i]</code>&nbsp;<em>with the maximum divisibility score</em>. If there is more than one integer with the maximum score, return the minimum of them.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,7,9,3,9], divisors = [5,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 5.
The divisibility score of divisors[1] is 1 since nums[0] is divisible by 2.
The divisibility score of divisors[2] is 3 since nums[2], nums[3], and nums[4] are divisible by 3.
Since divisors[2] has the maximum divisibility score, we return it.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [20,14,21,10], divisors = [5,7,5]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 2 since nums[0] and nums[3] are divisible by 5.
The divisibility score of divisors[1] is 2 since nums[1] and nums[2] are divisible by 7.
The divisibility score of divisors[2] is 2 since nums[0] and nums[3] are divisible by 5.
Since divisors[0], divisors[1], and divisors[2] all have the maximum divisibility score, we return the minimum of them (i.e., divisors[2]).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [12], divisors = [10,16]
<strong>Output:</strong> 10
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 10.
The divisibility score of divisors[1] is 0 since no number in nums is divisible by 16.
Since divisors[0] and divisors[1] both have the maximum divisibility score, we return the minimum of them (i.e., divisors[0]).
</pre>



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



<ul><li><code>1 &lt;= nums.length, divisors.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i], divisors[i] &lt;= 10<sup>9</sup></code></li></ul>



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



<p>Time complexity: O(m*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 maxDivScore(vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; divisors) {
    int maxScore = 0;
    int ans = INT_MAX;
    for (int d : divisors) {
      int score = 0;
      for (int x : nums) {
        score += x % d == 0;
      }
      if (score &gt; maxScore) {
        maxScore = score;
        ans = d;
      }
      if (score == maxScore)
        ans = min(ans, d);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/">花花酱 LeetCode 2644. Find the Maximum Divisibility Score</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-2644-find-the-maximum-divisibility-score/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1952. Three Divisors</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1952-three-divisors/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1952-three-divisors/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 20:55:01 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[divisor]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[sqrt]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9317</guid>

					<description><![CDATA[<p>Given an integer&#160;n, return&#160;true&#160;if&#160;n&#160;has&#160;exactly three positive divisors. Otherwise, return&#160;false. An integer&#160;m&#160;is a&#160;divisor&#160;of&#160;n&#160;if there exists an integer&#160;k&#160;such that&#160;n = k * m. Example 1: Input: n&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1952-three-divisors/">花花酱 LeetCode 1952. Three Divisors</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 an integer&nbsp;<code>n</code>, return&nbsp;<code>true</code><em>&nbsp;if&nbsp;</em><code>n</code><em>&nbsp;has&nbsp;<strong>exactly three positive divisors</strong>. Otherwise, return&nbsp;</em><code>false</code>.</p>



<p>An integer&nbsp;<code>m</code>&nbsp;is a&nbsp;<strong>divisor</strong>&nbsp;of&nbsp;<code>n</code>&nbsp;if there exists an integer&nbsp;<code>k</code>&nbsp;such that&nbsp;<code>n = k * m</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2
<strong>Output:</strong> false
<strong>Explantion:</strong> 2 has only two divisors: 1 and 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4
<strong>Output:</strong> true
<strong>Explantion:</strong> 4 has three divisors: 1, 2, and 4.
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li></ul>



<h2><strong>Solution: Enumerate divisors.</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:
  bool isThree(int n) {
    int c = 0;
    for (int i = 1; i &lt;= n; ++i)
      if (n % i == 0) ++c;        
    return c == 3;
  }
};</pre>
</div></div>



<h2><strong>Optimization</strong></h2>



<p>Only need to enumerate divisors up to sqrt(n). Special handle for the d * d == n case.</p>



<p>Time complexity: O(sqrt(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:
  bool isThree(int n) {
    int c = 0;
    for (int i = 1; i &lt;= sqrt(n); ++i)
      if (n % i == 0)
        c += 1 + (i * i != n);
    return c == 3;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1952-three-divisors/">花花酱 LeetCode 1952. Three Divisors</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/math/leetcode-1952-three-divisors/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1390. Four Divisors</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1390-four-divisors/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1390-four-divisors/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Mar 2020 21:06:57 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[divisor]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[sqrt]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6539</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1390-four-divisors/">花花酱 LeetCode 1390. Four Divisors</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 an integer array&nbsp;<code>nums</code>, return the sum of divisors of the integers in that array that have exactly four divisors.</p>



<p>If there is no such integer in the array, return&nbsp;<code>0</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [21,4,7]
<strong>Output:</strong> 32
<strong>Explanation:</strong>
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10^4</code></li><li><code>1 &lt;= nums[i] &lt;= 10^5</code></li></ul>



<h2><strong>Solution: Math</strong></h2>



<p>If a number is a perfect square (e.g. 9 = 3 * 3), it will have odd number of divisors. (9: 1, 3, 9).</p>



<p>Time complexity: O(sum(sqrt(num_i))<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 sumFourDivisors(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    for (int n : nums) {
      int r = sqrt(n);
      if (n &lt;= 4 || r * r == n) continue;
      int count = 2;
      int sum = 1 + n;
      for (int d = 2; d &lt;= r; ++d)
        if (n % d == 0) {
          count += 2;
          sum += n / d + d;
        }      
      if (count == 4) ans += sum;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1390-four-divisors/">花花酱 LeetCode 1390. Four Divisors</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/math/leetcode-1390-four-divisors/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
