<?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>math &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 02 Mar 2026 11:20:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.4</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>math &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>LeetCode 3512. Minimum Operations to Make Array Sum Divisible by K</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-3512-minimum-operations-to-make-array-sum-divisible-by-k/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-3512-minimum-operations-to-make-array-sum-divisible-by-k/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 16 Apr 2025 01:17:03 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10351</guid>

					<description><![CDATA[求合取余即可，余数就是答案。 Time complexity: O(n)Space complexity: O(1) [crayon-69a91b88dbdd7677702967/]]]></description>
										<content:encoded><![CDATA[
<p>求合取余即可，余数就是答案。</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int minOperations(vector&lt;int&gt;&amp; nums, int k) {
    return accumulate(begin(nums), end(nums), 0) % k;
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-3512-minimum-operations-to-make-array-sum-divisible-by-k/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 462. Minimum Moves to Equal Array Elements II</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-462-minimum-moves-to-equal-array-elements-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-462-minimum-moves-to-equal-array-elements-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Apr 2025 21:02:47 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[nth_element]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10289</guid>

					<description><![CDATA[不太喜欢这样的题目，你需要证明/猜测最优解是将所有数字转换成median/中位数。 使用nth_element找中位数，然后再循环一遍求合即可。 时间复杂度：O(n)空间复杂度：O(1) [crayon-69a91b88dcb57743067791/]]]></description>
										<content:encoded><![CDATA[
<p>不太喜欢这样的题目，你需要证明/猜测最优解是将所有数字转换成median/中位数。</p>



<p>使用nth_element找中位数，然后再循环一遍求合即可。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(1)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int minMoves2(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    nth_element(begin(nums), begin(nums) + n / 2, end(nums));
    const int median = nums[n / 2];
    return accumulate(begin(nums), end(nums), 0, [median](int s, int x) {
      return s + abs(x - median);
    });
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-462-minimum-moves-to-equal-array-elements-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2656. Maximum Sum With Exactly K Elements</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-2656-maximum-sum-with-exactly-k-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-2656-maximum-sum-with-exactly-k-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Apr 2023 14:58:44 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10022</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;integer array&#160;nums&#160;and an integer&#160;k. Your task is to perform the following operation&#160;exactly&#160;k&#160;times in order to maximize your score: Select an element&#160;m&#160;from&#160;nums. Remove&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>k</code>. Your task is to perform the following operation&nbsp;<strong>exactly</strong>&nbsp;<code>k</code>&nbsp;times in order to maximize your score:</p>



<ol class="wp-block-list"><li>Select an element&nbsp;<code>m</code>&nbsp;from&nbsp;<code>nums</code>.</li><li>Remove the selected element&nbsp;<code>m</code>&nbsp;from the array.</li><li>Add a new element with a value of&nbsp;<code>m + 1</code>&nbsp;to the array.</li><li>Increase your score by&nbsp;<code>m</code>.</li></ol>



<p>Return&nbsp;<em>the maximum score you can achieve after performing the operation exactly</em>&nbsp;<code>k</code>&nbsp;<em>times.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,5], k = 3
<strong>Output:</strong> 18
<strong>Explanation:</strong> We need to choose exactly 3 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
So, we will return 18.
It can be proven, that 18 is the maximum answer that we can achieve.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,5,5], k = 2
<strong>Output:</strong> 11
<strong>Explanation:</strong> We need to choose exactly 2 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
So, we will return 11.
It can be proven, that 11 is the maximum answer that we can achieve.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 100</code></li><li><code>1 &lt;= k &lt;= 100</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Greedy</strong></h2>



<p>Always to chose the largest element from the array.</p>



<p>We can find the largest element of the array m, then the total score will be<br>m + (m + 1) + (m + 2) + &#8230; + (m + k &#8211; 1),<br>We can use summation formula of arithmetic sequence to compute that in O(1)<br>ans = (m + (m + k &#8211; 1)) * k / 2</p>



<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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int maximizeSum(vector&lt;int&gt;&amp; nums, int k) {
    int m = *max_element(begin(nums), end(nums));
    return (m + m + k - 1) * k / 2;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-2656-maximum-sum-with-exactly-k-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2652. Sum Multiples</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2652-sum-multiples/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2652-sum-multiples/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 28 Apr 2023 04:23:08 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mod]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9992</guid>

					<description><![CDATA[Given a positive integer&#160;n, find the sum of all integers in the range&#160;[1, n]&#160;inclusive&#160;that are divisible by&#160;3,&#160;5, or&#160;7. Return&#160;an integer denoting the sum of all&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a positive integer&nbsp;<code>n</code>, find the sum of all integers in the range&nbsp;<code>[1, n]</code>&nbsp;<strong>inclusive</strong>&nbsp;that are divisible by&nbsp;<code>3</code>,&nbsp;<code>5</code>, or&nbsp;<code>7</code>.</p>



<p>Return&nbsp;<em>an integer denoting the sum of all numbers in the given range satisfying&nbsp;the constraint.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 7
<strong>Output:</strong> 21
<strong>Explanation:</strong> Numbers in the range <code>[1, 7]</code> that are divisible by <code>3</code>, <code>5,</code> or <code>7 </code>are <code>3, 5, 6, 7</code>. The sum of these numbers is <code>21</code>.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 10
<strong>Output:</strong> 40
<strong>Explanation:</strong> Numbers in the range <code>[1, 10] that are</code> divisible by <code>3</code>, <code>5,</code> or <code>7</code> are <code>3, 5, 6, 7, 9, 10</code>. The sum of these numbers is 40.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 9
<strong>Output:</strong> 30
<strong>Explanation:</strong> Numbers in the range <code>[1, 9]</code> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code> are <code>3, 5, 6, 7, 9</code>. The sum of these numbers is <code>30</code>.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Mod</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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int sumOfMultiples(int n) {
    int ans = 0;
    for (int i = 1; i &lt;= n; ++i) {
      if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0)
        ans += i;
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2652-sum-multiples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2582. Pass the Pillow</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2582-pass-the-pillow/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2582-pass-the-pillow/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Mar 2023 15:42:34 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mod]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9970</guid>

					<description><![CDATA[There are&#160;n&#160;people standing in a line labeled from&#160;1&#160;to&#160;n. The first person in the line is holding a pillow initially. Every second, the person holding the&#8230;]]></description>
										<content:encoded><![CDATA[
<p>There are&nbsp;<code>n</code>&nbsp;people standing in a line labeled from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.</p>



<ul class="wp-block-list"><li>For example, once the pillow reaches the&nbsp;<code>n<sup>th</sup></code>&nbsp;person they pass it to the&nbsp;<code>n - 1<sup>th</sup></code>&nbsp;person, then to the&nbsp;<code>n - 2<sup>th</sup></code>&nbsp;person and so on.</li></ul>



<p>Given the two positive integers&nbsp;<code>n</code>&nbsp;and&nbsp;<code>time</code>, return&nbsp;<em>the index of the person holding the pillow after&nbsp;</em><code>time</code><em>&nbsp;seconds</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, time = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 3 -&gt; 2.
Afer five seconds, the pillow is given to the 2<sup>nd</sup> person.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, time = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -&gt; 2 -&gt; 3.
Afer two seconds, the pillow is given to the 3<sup>r</sup><sup>d</sup> person.
</pre>



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



<ul class="wp-block-list"><li><code>2 &lt;= n &lt;= 1000</code></li><li><code>1 &lt;= time &lt;= 1000</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Math</strong></h2>



<p>It takes n &#8211; 1 seconds from 1 to n and takes another n &#8211; 1 seconds back from n to 1.<br>So one around takes 2 * (n &#8211; 1) seconds. We can mod time with 2 *  (n &#8211; 1).</p>



<p>After that if time &lt; n &#8211; 1 answer is time + 1, otherwise answer is n &#8211; (time &#8211; (n &#8211; 1))</p>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int passThePillow(int n, int time) {
    time %= (2 * (n - 1));
    return time &gt; n - 1 ? n - (time - (n - 1)) : time + 1;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2582-pass-the-pillow/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2575. Find the Divisibility Array of a String</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2575-find-the-divisibility-array-of-a-string/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2575-find-the-divisibility-array-of-a-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Feb 2023 06:07:28 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[big integer]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9967</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;string&#160;word&#160;of length&#160;n&#160;consisting of digits, and a positive integer&#160;m. The&#160;divisibility array&#160;div&#160;of&#160;word&#160;is an integer array of length&#160;n&#160;such that: div[i] = 1&#160;if the&#160;numeric value&#160;of&#160;word[0,...,i]&#160;is divisible&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;string&nbsp;<code>word</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;consisting of digits, and a positive integer&nbsp;<code>m</code>.</p>



<p>The&nbsp;<strong>divisibility array</strong>&nbsp;<code>div</code>&nbsp;of&nbsp;<code>word</code>&nbsp;is an integer array of length&nbsp;<code>n</code>&nbsp;such that:</p>



<ul class="wp-block-list"><li><code>div[i] = 1</code>&nbsp;if the&nbsp;<strong>numeric value</strong>&nbsp;of&nbsp;<code>word[0,...,i]</code>&nbsp;is divisible by&nbsp;<code>m</code>, or</li><li><code>div[i] = 0</code>&nbsp;otherwise.</li></ul>



<p>Return<em>&nbsp;the divisibility array of</em><em>&nbsp;</em><code>word</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "998244353", m = 3
<strong>Output:</strong> [1,1,0,0,0,1,1,0,0]
<strong>Explanation:</strong> There are only 4 prefixes that are divisible by 3: "9", "99", "998244", and "9982443".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "1010", m = 10
<strong>Output:</strong> [0,1,0,1]
<strong>Explanation:</strong> There are only 2 prefixes that are divisible by 10: "10", and "1010".
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>word.length == n</code></li><li><code>word</code>&nbsp;consists of digits from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>9</code></li><li><code>1 &lt;= m &lt;= 10<sup>9</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Big Integer Math</strong></h2>



<p>r = (r * 10 + word[i]) % m</p>



<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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; divisibilityArray(string word, int m) {
    vector&lt;int&gt; ans;
    long long r = 0;
    for (char c : word) {
      r = (r * 10 + c - '0') % m;
      ans.push_back(r == 0);
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2575-find-the-divisibility-array-of-a-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2469. Convert the Temperature</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2469-convert-the-temperature/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2469-convert-the-temperature/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 23 Nov 2022 04:05:27 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9894</guid>

					<description><![CDATA[You are given a non-negative floating point number rounded to two decimal places&#160;celsius, that denotes the&#160;temperature in Celsius. You should convert Celsius into&#160;Kelvin&#160;and&#160;Fahrenheit&#160;and return it&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a non-negative floating point number rounded to two decimal places&nbsp;<code>celsius</code>, that denotes the&nbsp;<strong>temperature in Celsius</strong>.</p>



<p>You should convert Celsius into&nbsp;<strong>Kelvin</strong>&nbsp;and&nbsp;<strong>Fahrenheit</strong>&nbsp;and return it as an array&nbsp;<code>ans = [kelvin, fahrenheit]</code>.</p>



<p>Return&nbsp;<em>the array&nbsp;<code>ans</code>.&nbsp;</em>Answers within&nbsp;<code>10<sup>-5</sup></code>&nbsp;of the actual answer will be accepted.</p>



<p><strong>Note that:</strong></p>



<ul class="wp-block-list"><li><code>Kelvin = Celsius + 273.15</code></li><li><code>Fahrenheit = Celsius * 1.80 + 32.00</code></li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> celsius = 36.50
<strong>Output:</strong> [309.65000,97.70000]
<strong>Explanation:</strong> Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> celsius = 122.11
<strong>Output:</strong> [395.26000,251.79800]
<strong>Explanation:</strong> Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
</pre>



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



<ul class="wp-block-list"><li><code>0 &lt;= celsius &lt;= 1000</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Follow the formulas</strong></h2>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;double&gt; convertTemperature(double celsius) {
    return {celsius + 273.15, celsius * 1.80 + 32.00};
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2469-convert-the-temperature/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2409. Count Days Spent Together</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2409-count-days-spent-together/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2409-count-days-spent-together/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Sep 2022 19:55:26 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[year]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9824</guid>

					<description><![CDATA[Alice and Bob are traveling to Rome for separate business meetings. You are given 4 strings&#160;arriveAlice,&#160;leaveAlice,&#160;arriveBob, and&#160;leaveBob. Alice will be in the city from the&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Alice and Bob are traveling to Rome for separate business meetings.</p>



<p>You are given 4 strings&nbsp;<code>arriveAlice</code>,&nbsp;<code>leaveAlice</code>,&nbsp;<code>arriveBob</code>, and&nbsp;<code>leaveBob</code>. Alice will be in the city from the dates&nbsp;<code>arriveAlice</code>&nbsp;to&nbsp;<code>leaveAlice</code>&nbsp;(<strong>inclusive</strong>), while Bob will be in the city from the dates&nbsp;<code>arriveBob</code>&nbsp;to&nbsp;<code>leaveBob</code>&nbsp;(<strong>inclusive</strong>). Each will be a 5-character string in the format&nbsp;<code>"MM-DD"</code>, corresponding to the month and day of the date.</p>



<p>Return<em>&nbsp;the total number of days that Alice and Bob are in Rome together.</em></p>



<p>You can assume that all dates occur in the&nbsp;<strong>same</strong>&nbsp;calendar year, which is&nbsp;<strong>not</strong>&nbsp;a leap year. Note that the number of days per month can be represented as:&nbsp;<code>[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
<strong>Output:</strong> 3
<strong>Explanation:</strong> Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no day when Alice and Bob are in Rome together, so we return 0.
</pre>



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



<ul class="wp-block-list"><li>All dates are provided in the format&nbsp;<code>"MM-DD"</code>.</li><li>Alice and Bob&#8217;s arrival dates are&nbsp;<strong>earlier than or equal to</strong>&nbsp;their leaving dates.</li><li>The given dates are valid dates of a&nbsp;<strong>non-leap</strong>&nbsp;year.</li></ul>



<h2 class="wp-block-heading"><strong>Solution: Math</strong></h2>



<p>Convert date to days of the year.</p>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
    array&lt;int, 12&gt; days{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    auto getDay = [&amp;](string date) {
      int mm = (date[0] - '0') * 10 + (date[1] - '0');
      int dd = (date[3] - '0') * 10 + (date[4] - '0');
      return accumulate(begin(days), begin(days) + mm - 1, dd);
    };
    
    int s = max(getDay(arriveAlice), getDay(arriveBob));
    int e = min(getDay(leaveAlice), getDay(leaveBob));
    return e &gt;= s ? e - s + 1 : 0;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2409-count-days-spent-together/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2396. Strictly Palindromic Number</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2396-strictly-palindromic-number%ef%bf%bc%ef%bf%bc/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2396-strictly-palindromic-number%ef%bf%bc%ef%bf%bc/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 08 Sep 2022 05:11:22 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[proof]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9800</guid>

					<description><![CDATA[An integer&#160;n&#160;is&#160;strictly palindromic&#160;if, for&#160;every&#160;base&#160;b&#160;between&#160;2&#160;and&#160;n - 2&#160;(inclusive), the string representation of the integer&#160;n&#160;in base&#160;b&#160;is&#160;palindromic. Given an integer&#160;n, return&#160;true&#160;if&#160;n&#160;is&#160;strictly palindromic&#160;and&#160;false&#160;otherwise. A string is&#160;palindromic&#160;if it reads the same&#8230;]]></description>
										<content:encoded><![CDATA[
<p>An integer&nbsp;<code>n</code>&nbsp;is&nbsp;<strong>strictly palindromic</strong>&nbsp;if, for&nbsp;<strong>every</strong>&nbsp;base&nbsp;<code>b</code>&nbsp;between&nbsp;<code>2</code>&nbsp;and&nbsp;<code>n - 2</code>&nbsp;(<strong>inclusive</strong>), the string representation of the integer&nbsp;<code>n</code>&nbsp;in base&nbsp;<code>b</code>&nbsp;is&nbsp;<strong>palindromic</strong>.</p>



<p>Given an integer&nbsp;<code>n</code>, return&nbsp;<code>true</code>&nbsp;<em>if&nbsp;</em><code>n</code><em>&nbsp;is&nbsp;<strong>strictly palindromic</strong>&nbsp;and&nbsp;</em><code>false</code><em>&nbsp;otherwise</em>.</p>



<p>A string is&nbsp;<strong>palindromic</strong>&nbsp;if it reads the same forward and backward.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 9
<strong>Output:</strong> false
<strong>Explanation:</strong> In base 2: 9 = 1001 (base 2), which is palindromic.
In base 3: 9 = 100 (base 3), which is not palindromic.
Therefore, 9 is not strictly palindromic so we return false.
Note that in bases 4, 5, 6, and 7, n = 9 is also not palindromic.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4
<strong>Output:</strong> false
<strong>Explanation:</strong> We only consider base 2: 4 = 100 (base 2), which is not palindromic.
Therefore, we return false.
</pre>



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



<ul class="wp-block-list"><li><code>4 &lt;= n &lt;= 10<sup>5</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Just return false</strong></h2>



<p>No such number.</p>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  bool isStrictlyPalindromic(int n) {
    return false;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2396-strictly-palindromic-number%ef%bf%bc%ef%bf%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2244. Minimum Rounds to Complete All Tasks</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2244-minimum-rounds-to-complete-all-tasks/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2244-minimum-rounds-to-complete-all-tasks/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Apr 2022 23:07:25 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9679</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;integer array&#160;tasks, where&#160;tasks[i]&#160;represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the&#160;same&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>tasks</code>, where&nbsp;<code>tasks[i]</code>&nbsp;represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the&nbsp;<strong>same difficulty level</strong>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;rounds required to complete all the tasks, or&nbsp;</em><code>-1</code><em>&nbsp;if it is not possible to complete all the tasks.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> tasks = [2,2,3,3,2,4,4,4,4,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> To complete all the tasks, a possible plan is:
- In the first round, you complete 3 tasks of difficulty level 2. 
- In the second round, you complete 2 tasks of difficulty level 3. 
- In the third round, you complete 3 tasks of difficulty level 4. 
- In the fourth round, you complete 2 tasks of difficulty level 4.  
It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> tasks = [2,3,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= tasks.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= tasks[i] &lt;= 10<sup>9</sup></code></li></ul>



<p></p>



<h2 class="wp-block-heading"><strong>Solution: Math</strong></h2>



<p>Count the frequency of each level. The only case that can not be finished is 1 task at some level. Otherwise we can always finish it by 2, 3 tasks at a time. </p>



<p>if n = 2: 2 => 1 round<br><meta charset="utf-8">if n = 3: 3 => 1 <meta charset="utf-8">round<br><meta charset="utf-8">if n = 4: 2 + 2 => 2 <meta charset="utf-8">rounds<br><meta charset="utf-8">if n = 5: 3 + 2 => 2 <meta charset="utf-8">rounds<br>&#8230;<br><meta charset="utf-8">if n = 3k, n % 3 == 0 : 3 + 3 + &#8230; + 3 = k rounds<br><meta charset="utf-8">if n = 3k + 1,  n % 3 == 1 : 3*(k &#8211; 1) + 2 + 2 = k + 1 rounds<br><meta charset="utf-8">if n = 3k + 2, n % 3 == 2 : 3*k + 2 = k + 1 rounds</p>



<p>We need (n + 2) / 3 rounds. </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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumRounds(vector&lt;int&gt;&amp; tasks) {
    unordered_map&lt;int, int&gt; m;
    for (int t : tasks) ++m[t];
    int ans = 0;
    for (auto [level, count] : m) {
      if (count == 1) return -1;
      ans += (count + 2) / 3;
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2244-minimum-rounds-to-complete-all-tasks/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2240. Number of Ways to Buy Pens and Pencils</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2240-number-of-ways-to-buy-pens-and-pencils/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2240-number-of-ways-to-buy-pens-and-pencils/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Apr 2022 05:26:31 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9656</guid>

					<description><![CDATA[You are given an integer&#160;total&#160;indicating the amount of money you have. You are also given two integers&#160;cost1&#160;and&#160;cost2&#160;indicating the price of a pen and pencil respectively.&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer&nbsp;<code>total</code>&nbsp;indicating the amount of money you have. You are also given two integers&nbsp;<code>cost1</code>&nbsp;and&nbsp;<code>cost2</code>&nbsp;indicating the price of a pen and pencil respectively. You can spend&nbsp;<strong>part or all</strong>&nbsp;of your money to buy multiple quantities (or none) of each kind of writing utensil.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>number of distinct ways</strong>&nbsp;you can buy some number of pens and pencils.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> total = 20, cost1 = 10, cost2 = 5
<strong>Output:</strong> 9
<strong>Explanation:</strong> The price of a pen is 10 and the price of a pencil is 5.
- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.
- If you buy 1 pen, you can buy 0, 1, or 2 pencils.
- If you buy 2 pens, you cannot buy any pencils.
The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> total = 5, cost1 = 10, cost2 = 10
<strong>Output:</strong> 1
<strong>Explanation:</strong> The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= total, cost1, cost2 &lt;= 10<sup>6</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: </strong></h2>



<p>Enumerate all possible ways to buy k pens, e.g. 0 pen, 1 pen, &#8230;, total / cost1.<br>The way to buy pencils are (total &#8211; k * cost1) / cost2 + 1.<br>ans = sum((total &#8211; k * cost1) / cost2 + 1)) for k = 0 to total / cost1.</p>



<p>Time complexity: O(total / cost1)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  long long waysToBuyPensPencils(int total, int cost1, int cost2) {
    long long ans = 0;
    for (long long k = 0; k &lt;= total / cost1; ++k)
      ans += (total - k * cost1) / cost2 + 1;
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2240-number-of-ways-to-buy-pens-and-pencils/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2178. Maximum Split of Positive Even Integers</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-2178-maximum-split-of-positive-even-integers/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-2178-maximum-split-of-positive-even-integers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Mar 2022 06:31:22 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9542</guid>

					<description><![CDATA[You are given an integer&#160;finalSum. Split it into a sum of a&#160;maximum&#160;number of&#160;unique&#160;positive even integers. For example, given&#160;finalSum = 12, the following splits are&#160;valid&#160;(unique positive&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer&nbsp;<code>finalSum</code>. Split it into a sum of a&nbsp;<strong>maximum</strong>&nbsp;number of&nbsp;<strong>unique</strong>&nbsp;positive even integers.</p>



<ul class="wp-block-list"><li>For example, given&nbsp;<code>finalSum = 12</code>, the following splits are&nbsp;<strong>valid</strong>&nbsp;(unique positive even integers summing up to&nbsp;<code>finalSum</code>):&nbsp;<code>(12)</code>,&nbsp;<code>(2 + 10)</code>,&nbsp;<code>(2 + 4 + 6)</code>, and&nbsp;<code>(4 + 8)</code>. Among them,&nbsp;<code>(2 + 4 + 6)</code>&nbsp;contains the maximum number of integers. Note that&nbsp;<code>finalSum</code>&nbsp;cannot be split into&nbsp;<code>(2 + 2 + 4 + 4)</code>&nbsp;as all the numbers should be unique.</li></ul>



<p>Return&nbsp;<em>a list of integers that represent a valid split containing a&nbsp;<strong>maximum</strong>&nbsp;number of integers</em>. If no valid split exists for&nbsp;<code>finalSum</code>, return&nbsp;<em>an&nbsp;<strong>empty</strong>&nbsp;list</em>. You may return the integers in&nbsp;<strong>any</strong>&nbsp;order.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> finalSum = 12
<strong>Output:</strong> [2,4,6]
<strong>Explanation:</strong> The following are valid splits: <code>(12)</code>, <code>(2 + 10)</code>, <code>(2 + 4 + 6)</code>, and <code>(4 + 8)</code>.
(2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6].
Note that [2,6,4], [6,2,4], etc. are also accepted.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> finalSum = 7
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no valid splits for the given finalSum.
Thus, we return an empty array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> finalSum = 28
<strong>Output:</strong> [6,8,2,12]
<strong>Explanation:</strong> The following are valid splits: <code>(2 + 26)</code>, <code>(6 + 8 + 2 + 12)</code>, and <code>(4 + 24)</code>. 
<code>(6 + 8 + 2 + 12)</code> has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= finalSum &lt;= 10<sup>10</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Greedy</strong></h2>



<p>The get the maximum number of elements, we must use the smallest numbers.</p>



<p>[2, 4, 6, &#8230;, 2k, x], where x > 2k<br>let s = 2 + 4 + &#8230; + 2k, x = num &#8211; s<br>since num is odd and s is also odd, so thus x = num &#8211; s.</p>



<p>Time complexity: O(sqrt(num)) for constructing outputs.<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;long long&gt; maximumEvenSplit(long long finalSum) {
    if (finalSum &amp; 1) return {};
    vector&lt;long long&gt; ans;
    long long s = 0;
    for (long long i = 2; s + i &lt;= finalSum; s += i, i += 2)
      ans.push_back(i);
    ans.back() += (finalSum - s);
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-2178-maximum-split-of-positive-even-integers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2177. Find Three Consecutive Integers That Sum to a Given Number</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2177-find-three-consecutive-integers-that-sum-to-a-given-number/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2177-find-three-consecutive-integers-that-sum-to-a-given-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Mar 2022 05:24:26 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9538</guid>

					<description><![CDATA[Given an integer&#160;num, return&#160;three consecutive integers (as a sorted array)&#160;that&#160;sum&#160;to&#160;num. If&#160;num&#160;cannot be expressed as the sum of three consecutive integers, return&#160;an&#160;empty&#160;array. Example 1: Input: num&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given an integer&nbsp;<code>num</code>, return&nbsp;<em>three consecutive integers (as a sorted array)</em><em>&nbsp;that&nbsp;<strong>sum</strong>&nbsp;to&nbsp;</em><code>num</code>. If&nbsp;<code>num</code>&nbsp;cannot be expressed as the sum of three consecutive integers, return<em>&nbsp;an&nbsp;<strong>empty</strong>&nbsp;array.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 33
<strong>Output:</strong> [10,11,12]
<strong>Explanation:</strong> 33 can be expressed as 10 + 11 + 12 = 33.
10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 4
<strong>Output:</strong> []
<strong>Explanation:</strong> There is no way to express 4 as the sum of 3 consecutive integers.
</pre>



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



<ul class="wp-block-list"><li><code>0 &lt;= num &lt;= 10<sup>15</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Math</strong></h2>



<p>(x / 3 &#8211; 1) + (x / 3) +  (x / 3 + 1) == 3x == num, num must be divisible by 3.</p>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;long long&gt; sumOfThree(long long num) {
    if (num % 3) return {};
    return {num / 3 - 1, num / 3, num / 3 + 1};    
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2177-find-three-consecutive-integers-that-sum-to-a-given-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2183. Count Array Pairs Divisible by K</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2183-count-array-pairs-divisible-by-k/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2183-count-array-pairs-divisible-by-k/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Mar 2022 03:49:45 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[gcd]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9531</guid>

					<description><![CDATA[Given a&#160;0-indexed&#160;integer array&#160;nums&#160;of length&#160;n&#160;and an integer&#160;k, return&#160;the&#160;number of pairs&#160;(i, j)&#160;such that: 0 &#60;= i &#60; j &#60;= n - 1&#160;and nums[i] * nums[j]&#160;is divisible by&#160;k.&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;and an integer&nbsp;<code>k</code>, return&nbsp;<em>the&nbsp;<strong>number of pairs</strong></em>&nbsp;<code>(i, j)</code>&nbsp;<em>such that:</em></p>



<ul class="wp-block-list"><li><code>0 &lt;= i &lt; j &lt;= n - 1</code>&nbsp;<em>and</em></li><li><code>nums[i] * nums[j]</code>&nbsp;<em>is divisible by</em>&nbsp;<code>k</code>.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,5], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> 
The 7 pairs of indices whose corresponding products are divisible by 2 are
(0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.    
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 0
<strong>Explanation:</strong> There does not exist any pair of indices whose corresponding product is divisible by 5.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Math</strong></h2>



<p>a * b % k == 0 &lt;=> gcd(a, k) * gcd(b, k) == 0</p>



<p>Use a counter of gcd(x, k) so far to compute the number of pairs.</p>



<p>Time complexity: O(n*f), where f is the number of gcds, f &lt;= 128 for x &lt;= 1e5<br>Space complexity: O(<meta charset="utf-8">f)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  long long countPairs(vector&lt;int&gt;&amp; nums, int k) {
    unordered_map&lt;int, int&gt; m;
    long long ans = 0;
    for (int x : nums) {
      long long d1 = gcd(x, k);
      for (const auto&amp; [d2, c] : m)
        if (d1 * d2 % k == 0) ans += c;
      ++m[d1];
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2183-count-array-pairs-divisible-by-k/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2169. Count Operations to Obtain Zero</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-2169-count-operations-to-obtain-zero/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-2169-count-operations-to-obtain-zero/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Feb 2022 13:59:06 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9499</guid>

					<description><![CDATA[You are given two&#160;non-negative&#160;integers&#160;num1&#160;and&#160;num2. In one&#160;operation, if&#160;num1 &#62;= num2, you must subtract&#160;num2&#160;from&#160;num1, otherwise subtract&#160;num1&#160;from&#160;num2. For example, if&#160;num1 = 5&#160;and&#160;num2 = 4, subtract&#160;num2&#160;from&#160;num1, thus obtaining&#160;num1 =&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given two&nbsp;<strong>non-negative</strong>&nbsp;integers&nbsp;<code>num1</code>&nbsp;and&nbsp;<code>num2</code>.</p>



<p>In one&nbsp;<strong>operation</strong>, if&nbsp;<code>num1 &gt;= num2</code>, you must subtract&nbsp;<code>num2</code>&nbsp;from&nbsp;<code>num1</code>, otherwise subtract&nbsp;<code>num1</code>&nbsp;from&nbsp;<code>num2</code>.</p>



<ul class="wp-block-list"><li>For example, if&nbsp;<code>num1 = 5</code>&nbsp;and&nbsp;<code>num2 = 4</code>, subtract&nbsp;<code>num2</code>&nbsp;from&nbsp;<code>num1</code>, thus obtaining&nbsp;<code>num1 = 1</code>&nbsp;and&nbsp;<code>num2 = 4</code>. However, if&nbsp;<code>num1 = 4</code>&nbsp;and&nbsp;<code>num2 = 5</code>, after one operation,&nbsp;<code>num1 = 4</code>&nbsp;and&nbsp;<code>num2 = 1</code>.</li></ul>



<p>Return&nbsp;<em>the&nbsp;<strong>number of operations</strong>&nbsp;required to make either</em>&nbsp;<code>num1 = 0</code>&nbsp;<em>or</em>&nbsp;<code>num2 = 0</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num1 = 2, num2 = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 
- Operation 1: num1 = 2, num2 = 3. Since num1 &lt; num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
- Operation 2: num1 = 2, num2 = 1. Since num1 &gt; num2, we subtract num2 from num1.
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
So the total number of operations required is 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num1 = 10, num2 = 10
<strong>Output:</strong> 1
<strong>Explanation:</strong> 
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
So the total number of operations required is 1.
</pre>



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



<ul class="wp-block-list"><li><code>0 &lt;= num1, num2 &lt;= 10<sup>5</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution 1: Simulation</strong></h2>



<p>Time complexity: O(max(n,m) /  min(n, m))<br>Space complexity: O(1)</p>



<p>No code</p>



<h2 class="wp-block-heading"><strong>Solution 2: Simualtion + Math</strong></h2>



<p>For the case of 100, 3<br>100 &#8211; 3 = 97<br>97 &#8211; 3 = 94<br>&#8230;<br>4 &#8211; 3 = 1<br>Swap<br>3 &#8211; 1 = 2<br>2 &#8211; 1 = 1<br>1 &#8211; 1 = 0<br>It takes 36 steps.</p>



<p>We can do 100 / 3 to skip 33 steps <br>100 %= 3 = 1<br>3 / 1 = 3 to skip 3 steps<br>3 %= 1 = 0<br>total is 33 + 3 = 36.</p>



<p>Time complexity: O(logn) ? <br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int countOperations(int num1, int num2) {
    int ans = 0;
    while (num1 &amp;&amp; num2) {
      if (num1 &lt; num2) swap(num1, num2);
      ans += num1 / num2;
      num1 %= num2;      
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-2169-count-operations-to-obtain-zero/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
