<?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>odd Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/odd/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/odd/</link>
	<description></description>
	<lastBuildDate>Sun, 16 Aug 2020 04:26:48 +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>odd Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/odd/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1550. Three Consecutive Odds</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1550-three-consecutive-odds/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1550-three-consecutive-odds/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 16 Aug 2020 04:25:28 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[counting]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[odd]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7247</guid>

					<description><![CDATA[<p>Given an integer array&#160;arr, return&#160;true&#160;if there are three consecutive odd numbers in the array. Otherwise, return&#160;false. Example 1: Input: arr = [2,6,4,1] Output: false Explanation:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1550-three-consecutive-odds/">花花酱 LeetCode 1550. Three Consecutive Odds</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>arr</code>, return&nbsp;<code>true</code>&nbsp;if there are three consecutive odd numbers in the array. Otherwise, return&nbsp;<code>false</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [2,6,4,1]
<strong>Output:</strong> false
<strong>Explanation:</strong> There are no three consecutive odds.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,34,3,4,5,7,23,12]
<strong>Output:</strong> true
<strong>Explanation:</strong> [5,7,23] are three consecutive odds.
</pre>



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



<ul><li><code>1 &lt;= arr.length &lt;= 1000</code></li><li><code>1 &lt;= arr[i] &lt;= 1000</code></li></ul>



<h2><strong>Solution: Counting</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">class Solution {
public:
  bool threeConsecutiveOdds(vector&lt;int&gt;&amp; arr) {       
    int count = 0;
    for (int x : arr) {
      if (x &amp; 1) {
        if (++count == 3) return true;
      } else {
        count = 0;
      }
    }
    return false;      
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1550-three-consecutive-odds/">花花酱 LeetCode 1550. Three Consecutive Odds</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-1550-three-consecutive-odds/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1524. Number of Sub-arrays With Odd Sum</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1524-number-of-sub-arrays-with-odd-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1524-number-of-sub-arrays-with-odd-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 25 Jul 2020 18:24:50 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[even]]></category>
		<category><![CDATA[odd]]></category>
		<category><![CDATA[prefix sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7133</guid>

					<description><![CDATA[<p>Given an array of integers&#160;arr. Return&#160;the number of sub-arrays&#160;with&#160;odd&#160;sum. As the answer may grow large, the answer&#160;must be&#160;computed modulo&#160;10^9 + 7. Example 1: Input: arr&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1524-number-of-sub-arrays-with-odd-sum/">花花酱 LeetCode 1524. Number of Sub-arrays With Odd Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1524. Number of Sub-arrays With Odd Sum - 刷题找工作 EP345" width="500" height="281" src="https://www.youtube.com/embed/vGTm8rjlDTQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given an array of integers&nbsp;<code>arr</code>. Return&nbsp;<em>the number of sub-arrays</em>&nbsp;with&nbsp;<strong>odd</strong>&nbsp;sum.</p>



<p>As the answer may grow large, the answer&nbsp;<strong>must be</strong>&nbsp;computed modulo&nbsp;<code>10^9 + 7</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,3,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]
All sub-arrays sum are [1,4,9,3,8,5].
Odd sums are [1,9,3,5] so the answer is 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [2,4,6]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]
All sub-arrays sum are [2,6,12,4,10,6].
All sub-arrays have even sum and the answer is 0.
</pre>



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



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [100,100,99,99]
<strong>Output:</strong> 4
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [7]
<strong>Output:</strong> 1
</pre>



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



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



<h2><strong>Solution: DP</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-1.png" alt="" class="wp-image-7138" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-2.png" alt="" class="wp-image-7140" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/07/1524-ep345-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>We would like to know how many subarrays <strong>end with arr[i]</strong> have odd or even sums.<br></p>



<p>dp[i][0] := # end with arr[i] has even sum<br>dp[i][1] := # end with arr[i] has even sum</p>



<p>if arr[i] is even:</p>



<p>&nbsp;&nbsp;dp[i][0]=dp[i-1][0] + 1, dp[i][1]=dp[i-1][1]</p>



<p>else:</p>



<p>&nbsp;&nbsp;dp[i][1]=dp[i-1][0], dp[i][0]=dp[i-1][0] + 1</p>



<p>ans = sum(dp[i][1])</p>



<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int numOfSubarrays(vector&lt;int&gt;&amp; arr) {
    const int n = arr.size();
    constexpr int kMod = 1e9 + 7;
    // dp[i][0] # of subarrays ends with a[i-1] have even sums.
    // dp[i][1] # of subarrays ends with a[i-1] have odd sums.
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(2));
    long ans = 0;
    for (int i = 1; i &lt;= n; ++i) {
      if (arr[i - 1] &amp; 1) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = dp[i - 1][0] + 1;
      } else {
        dp[i][0] = dp[i - 1][0] + 1;
        dp[i][1] = dp[i - 1][1];
      }
      ans += dp[i][1];
    }
    return ans % kMod;
  }
};</pre>

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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public int numOfSubarrays(int[] arr) {
    long ans = 0, odd = 0, even = 0;
    for (int x : arr) {
      even += 1;
      if (x % 2 == 1) {
        long t = even; even = odd; odd = t;
      }
      ans += odd;
    }
    return (int)(ans % (int)(1e9 + 7));
  }
}</pre>

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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def numOfSubarrays(self, arr: List[int]) -&gt; int:
    ans, odd, even = 0, 0, 0
    for x in arr:
      if x &amp; 1:
        odd, even = even + 1, odd
      else:
        odd, even = odd, even + 1
      ans += odd
    return ans % int(1e9 + 7)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1524-number-of-sub-arrays-with-odd-sum/">花花酱 LeetCode 1524. Number of Sub-arrays With Odd Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1524-number-of-sub-arrays-with-odd-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1523. Count Odd Numbers in an Interval Range</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1523-count-odd-numbers-in-an-interval-range/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1523-count-odd-numbers-in-an-interval-range/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 25 Jul 2020 17:27:02 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[even]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[odd]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7130</guid>

					<description><![CDATA[<p>Given two non-negative integers&#160;low&#160;and&#160;high. Return the&#160;count of odd numbers between&#160;low&#160;and&#160;high&#160;(inclusive). Example 1: Input: low = 3, high = 7 Output: 3 Explanation: The odd numbers&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1523-count-odd-numbers-in-an-interval-range/">花花酱 LeetCode 1523. Count Odd Numbers in an Interval Range</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given two non-negative integers&nbsp;<code>low</code>&nbsp;and&nbsp;<code>high</code>. Return the&nbsp;<em>count of odd numbers between&nbsp;</em><code>low</code><em>&nbsp;and&nbsp;</em><code>high</code><em>&nbsp;(inclusive)</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> low = 3, high = 7
<strong>Output:</strong> 3
<strong>Explanation: </strong>The odd numbers between 3 and 7 are [3,5,7].</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> low = 8, high = 10
<strong>Output:</strong> 1
<strong>Explanation: </strong>The odd numbers between 8 and 10 are [9].</pre>



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



<ul><li><code>0 &lt;= low &lt;= high&nbsp;&lt;= 10^9</code></li></ul>



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



<p>The count of odd numbers between [1, low &#8211; 1] is low / 2<br>e.g. low = 6, we have [1,3,5] in range [1, 5] and count is 6/2 = 3.<br>The count of odd numbers between [1, high] is (high + 1) / 2<br>e.g. high = 7, we have [1,3,5,7] in range [1, 7] and count is (7+1) / 2 = 4</p>



<p>Then the count of odd numbers in range [low, high] = count(1, high) &#8211; count(1, low-1)<br>e.g. in range [6, 7] we only have [7], count: 4 &#8211; 3 = 1</p>



<p>ans = (high + 1) / 2 &#8211; low / 2</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="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countOdds(int low, int high) {
    return (high + 1) / 2 - low / 2;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1523-count-odd-numbers-in-an-interval-range/">花花酱 LeetCode 1523. Count Odd Numbers in an Interval Range</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-1523-count-odd-numbers-in-an-interval-range/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 905. Sort Array By Parity</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-905-sort-array-by-parity/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-905-sort-array-by-parity/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 16 Sep 2018 03:32:00 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[even]]></category>
		<category><![CDATA[odd]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3984</guid>

					<description><![CDATA[<p>Problem Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-905-sort-array-by-parity/">花花酱 LeetCode 905. Sort Array By Parity</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem</h1>
<p>Given an array <code>A</code> of non-negative integers, return an array consisting of all the even elements of <code>A</code>, followed by all the odd elements of <code>A</code>.</p>
<p>You may return any answer array that satisfies this condition.</p>
<div>
<p><strong>Example 1:</strong></p><pre class="crayon-plain-tag">&lt;strong&gt;Input: &lt;/strong&gt;&lt;span id=&quot;example-input-1-1&quot;&gt;[3,1,2,4]&lt;/span&gt;
&lt;strong&gt;Output: &lt;/strong&gt;&lt;span id=&quot;example-output-1&quot;&gt;[2,4,3,1]&lt;/span&gt;
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.</pre><p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= A.length &lt;= 5000</code></li>
<li><code>0 &lt;= A[i] &lt;= 5000</code></li>
</ol>
<h1><strong>Solution 1: Split Odd/Even</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 44 ms
class Solution {
public:
  vector&lt;int&gt; sortArrayByParity(vector&lt;int&gt;&amp; A) {
    vector&lt;int&gt; even;
    vector&lt;int&gt; odd;
    for (int a : A) {
      if (a % 2) odd.push_back(a);
      else even.push_back(a);
    }
    even.insert(end(even), begin(odd), end(odd));
    return even;
  }
};</pre><p></div></div></p>
</div>
<h1><strong>Solution 2: Stable sort by key % 2</strong></h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(1) in-place</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 52 ms
class Solution {
public:
  vector&lt;int&gt; sortArrayByParity(vector&lt;int&gt;&amp; A) {
    stable_sort(begin(A), end(A), [](int a, int b){
      return a % 2 &lt; b % 2;
    });
    return A;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-905-sort-array-by-parity/">花花酱 LeetCode 905. Sort Array By Parity</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-905-sort-array-by-parity/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
