<?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>mod &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/mod/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>mod &#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-69a7c97e2c873044101592/]]]></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 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 1894. Find the Student that Will Replace the Chalk</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1894-find-the-student-that-will-replace-the-chalk/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1894-find-the-student-that-will-replace-the-chalk/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 10 Aug 2021 02:44:29 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mod]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8552</guid>

					<description><![CDATA[There are&#160;n&#160;students in a class numbered from&#160;0&#160;to&#160;n - 1. The teacher will give each student a problem starting with the student number&#160;0, then the student&#8230;]]></description>
										<content:encoded><![CDATA[
<p>There are&nbsp;<code>n</code>&nbsp;students in a class numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n - 1</code>. The teacher will give each student a problem starting with the student number&nbsp;<code>0</code>, then the student number&nbsp;<code>1</code>, and so on until the teacher reaches the student number&nbsp;<code>n - 1</code>. After that, the teacher will restart the process, starting with the student number&nbsp;<code>0</code>&nbsp;again.</p>



<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>chalk</code>&nbsp;and an integer&nbsp;<code>k</code>. There are initially&nbsp;<code>k</code>&nbsp;pieces of chalk. When the student number&nbsp;<code>i</code>&nbsp;is given a problem to solve, they will use&nbsp;<code>chalk[i]</code>&nbsp;pieces of chalk to solve that problem. However, if the current number of chalk pieces is&nbsp;<strong>strictly less</strong>&nbsp;than&nbsp;<code>chalk[i]</code>, then the student number&nbsp;<code>i</code>&nbsp;will be asked to&nbsp;<strong>replace</strong>&nbsp;the chalk.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>index</strong>&nbsp;of the student that will&nbsp;<strong>replace</strong>&nbsp;the chalk</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> chalk = [5,1,5], k = 22
<strong>Output:</strong> 0
<strong>Explanation: </strong>The students go in turns as follows:
- Student number 0 uses 5 chalk, so k = 17.
- Student number 1 uses 1 chalk, so k = 16.
- Student number 2 uses 5 chalk, so k = 11.
- Student number 0 uses 5 chalk, so k = 6.
- Student number 1 uses 1 chalk, so k = 5.
- Student number 2 uses 5 chalk, so k = 0.
Student number 0 does not have enough chalk, so they will have to replace it.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> chalk = [3,4,1,2], k = 25
<strong>Output:</strong> 1
<strong>Explanation: </strong>The students go in turns as follows:
- Student number 0 uses 3 chalk so k = 22.
- Student number 1 uses 4 chalk so k = 18.
- Student number 2 uses 1 chalk so k = 17.
- Student number 3 uses 2 chalk so k = 15.
- Student number 0 uses 3 chalk so k = 12.
- Student number 1 uses 4 chalk so k = 8.
- Student number 2 uses 1 chalk so k = 7.
- Student number 3 uses 2 chalk so k = 5.
- Student number 0 uses 3 chalk so k = 2.
Student number 1 does not have enough chalk, so they will have to replace it.
</pre>



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



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



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



<p>Sum up all the students. k %= sum to skip all the middle rounds.</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 chalkReplacer(vector&lt;int&gt;&amp; chalk, int k) {    
    long sum = accumulate(begin(chalk), end(chalk), 0L);
    k %= sum;
    for (size_t i = 0; i &lt; chalk.size(); ++i)
      if ((k -= chalk[i]) &lt; 0) return i;
    return -1;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-1894-find-the-student-that-will-replace-the-chalk/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1560. Most Visited Sector in a Circular Track</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1560-most-visited-sector-in-a-circular-track/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1560-most-visited-sector-in-a-circular-track/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 23 Aug 2020 17:51:03 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[round]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7294</guid>

					<description><![CDATA[Given an integer&#160;n&#160;and an integer array&#160;rounds.&#160;We&#160;have a circular track which consists of&#160;n&#160;sectors labeled from&#160;1&#160;to&#160;n. A marathon will be held on this track, the marathon consists&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given an integer&nbsp;<code>n</code>&nbsp;and an integer array&nbsp;<code>rounds</code>.&nbsp;We&nbsp;have a circular track which consists of&nbsp;<code>n</code>&nbsp;sectors labeled from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>n</code>. A marathon will be held on this track, the marathon consists of&nbsp;<code>m</code>&nbsp;rounds. The&nbsp;<code>i<sup>th</sup></code>&nbsp;round starts at sector&nbsp;<code>rounds[i - 1]</code>&nbsp;and ends at sector&nbsp;<code>rounds[i]</code>. For example, round 1 starts at sector&nbsp;<code>rounds[0]</code>&nbsp;and ends at sector&nbsp;<code>rounds[1]</code></p>



<p>Return&nbsp;<em>an array of the most visited sectors</em>&nbsp;sorted in&nbsp;<strong>ascending</strong>&nbsp;order.</p>



<p>Notice that you&nbsp;circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).</p>



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, rounds = [1,3,1,2]
<strong>Output:</strong> [1,2]
<strong>Explanation:</strong> The marathon starts at sector 1. The order of the visited sectors is as follows:
1 --&gt; 2 --&gt; 3 (end of round 1) --&gt; 4 --&gt; 1 (end of round 2) --&gt; 2 (end of round 3 and the marathon)
We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, rounds = [2,1,2,1,2,1,2,1,2]
<strong>Output:</strong> [2]
</pre>



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



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



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



<ul class="wp-block-list"><li><code>2 &lt;= n &lt;= 100</code></li><li><code>1 &lt;= m &lt;= 100</code></li><li><code>rounds.length == m + 1</code></li><li><code>1 &lt;= rounds[i] &lt;= n</code></li><li><code>rounds[i] != rounds[i + 1]</code>&nbsp;for&nbsp;<code>0 &lt;= i &lt; m</code></li></ul>



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



<p>Time complexity: O(m*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">class Solution {
public:
  vector&lt;int&gt; mostVisited(int n, vector&lt;int&gt;&amp; rounds) {
    vector&lt;int&gt; counts(n);
    counts[rounds[0] - 1] = 1;
    for (int i = 1; i &lt; rounds.size(); ++i)
      for (int s = rounds[i - 1]; ; ++s) {
        ++counts[s %= n];
        if (s == rounds[i] - 1) break;
      }
    const int max_count = *max_element(begin(counts), end(counts));
    vector&lt;int&gt; ans;
    for (int i = 0; i &lt; n; ++i)      
      if (counts[i] == max_count) ans.push_back(i + 1);    
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-1560-most-visited-sector-in-a-circular-track/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1497. Check If Array Pairs Are Divisible by k</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1497-check-if-array-pairs-are-divisible-by-k/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1497-check-if-array-pairs-are-divisible-by-k/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Jun 2020 07:50:56 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[frequency]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mod]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6996</guid>

					<description><![CDATA[Given an array of integers&#160;arr&#160;of even length&#160;n&#160;and an integer&#160;k. We want to divide the array into exactly&#160;n /&#160;2&#160;pairs such that the sum of each pair&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;of even length&nbsp;<code>n</code>&nbsp;and an integer&nbsp;<code>k</code>.</p>



<p>We want to divide the array into exactly&nbsp;<code>n /&nbsp;2</code>&nbsp;pairs such that the sum of each pair is divisible by&nbsp;<code>k</code>.</p>



<p>Return&nbsp;<em>True</em>&nbsp;If you can find a way to do that or&nbsp;<em>False</em>&nbsp;otherwise.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,3,4,5,10,6,7,8,9], k = 5
<strong>Output:</strong> true
<strong>Explanation:</strong> Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,3,4,5,6], k = 7
<strong>Output:</strong> true
<strong>Explanation:</strong> Pairs are (1,6),(2,5) and(3,4).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,3,4,5,6], k = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> You can try all possible pairs to see that there is no way to divide arr into 3 pairs each with sum divisible by 10.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [-10,10], k = 2
<strong>Output:</strong> true
</pre>



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



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



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



<ul class="wp-block-list"><li><code>arr.length == n</code></li><li><code>1 &lt;= n &lt;= 10^5</code></li><li><code>n</code>&nbsp;is even.</li><li><code>-10^9 &lt;= arr[i] &lt;= 10^9</code></li><li><code>1 &lt;= k &lt;= 10^5</code></li></ul>



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



<p>Count the frequency of (x % k + k) % k.<br>f[0] should be even (zero is also even)<br>f[1] = f[k -1] ((1 + k &#8211; 1) % k == 0)<br>f[2] = f[k -2] ((2 + k &#8211; 2) % k == 0)<br>&#8230;<br>Time complexity: O(n)<br>Space complexity: O(k)</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 canArrange(vector&lt;int&gt;&amp; arr, int k) {
    vector&lt;int&gt; f(k);
    for (int x : arr) ++f[(x % k + k) % k];
    for (int i = 1; i &lt; k; ++i)
      if (f[i] != f[k - i]) return false;
    return f[0] % 2 == 0;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-1497-check-if-array-pairs-are-divisible-by-k/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 728. Self Dividing Numbers</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-728-self-dividing-numbers/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-728-self-dividing-numbers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 19 Nov 2017 16:13:34 +0000</pubDate>
				<category><![CDATA[Easy]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[reminder]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=845</guid>

					<description><![CDATA[Problem: A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128&#8230;]]></description>
										<content:encoded><![CDATA[<p><iframe title="花花酱 LeetCode 728. Self Dividing Numbers  - 刷题找工作 EP111" width="500" height="375" src="https://www.youtube.com/embed/Px8TABDC7ag?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>A <i>self-dividing number</i> is a number that is divisible by every digit it contains.</p>
<p>For example, 128 is a self-dividing number because <code>128 % 1 == 0</code>, <code>128 % 2 == 0</code>, and <code>128 % 8 == 0</code>.</p>
<p>Also, a self-dividing number is not allowed to contain the digit zero.</p>
<p>Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.</p>
<p><b>Example 1:</b></p><pre class="urvanov-syntax-highlighter-plain-tag">Input: 
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]</pre><p><b>Note:</b></p>
<ul>
<li>The boundaries of each input argument are <code>1 &lt;= left &lt;= right &lt;= 10000</code>.</li>
</ul>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<strong>Idea:</strong></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-851" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p>Brute Force</p>
<p>Time Complexity: O(n)</p>
<p>Space Complexity: O(1)</p>
<p>&nbsp;</p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
// Runtime: 3 ms
class Solution {
public:
    vector&lt;int&gt; selfDividingNumbers(int left, int right) {
        vector&lt;int&gt; ans;
        for (int n = left; n &lt;= right; ++n) {
            int t = n;
            bool valid = true;
            while (t &amp;&amp; valid) {
                const int r = t % 10;
                if (r == 0 || n % r)
                    valid = false;
                t /= 10;
            }
            if (valid) ans.push_back(n);
        }
        return ans;
    }
};</pre><p>String</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
// Runtime: 6 ms
class Solution {
public:
    vector&lt;int&gt; selfDividingNumbers(int left, int right) {
        vector&lt;int&gt; ans;
        for (int n = left; n &lt;= right; ++n) {
            const string t = std::to_string(n);
            bool valid = true;
            for (const char c : t) {
                if (c == '0' || n % (c - '0')) {
                    valid = false;
                    break;
                }
            }
            if (valid) ans.push_back(n);
        }
        return ans;
    }
};</pre><p>&nbsp;</p>
<p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/math/leetcode-611-valid-triangle-number/">[解题报告] LeetCode 611. Valid Triangle Number</a></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-728-self-dividing-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
