<?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>missing Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/missing/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/missing/</link>
	<description></description>
	<lastBuildDate>Sun, 28 Nov 2021 05:12:58 +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>missing Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/missing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 136. Single Number</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 05:04:42 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[missing]]></category>
		<category><![CDATA[O(1) space]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8843</guid>

					<description><![CDATA[<p>Given a&#160;non-empty&#160;array of integers&#160;nums, every element appears&#160;twice&#160;except for one. Find that single one. You must&#160;implement a solution with a linear runtime complexity and use&#160;only constant&#160;extra&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/">花花酱 LeetCode 136. Single Number</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 a&nbsp;<strong>non-empty</strong>&nbsp;array of integers&nbsp;<code>nums</code>, every element appears&nbsp;<em>twice</em>&nbsp;except for one. Find that single one.</p>



<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p>



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



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



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



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



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



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



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



<ul><li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li><li><code>-3 * 10<sup>4</sup>&nbsp;&lt;= nums[i] &lt;= 3 * 10<sup>4</sup></code></li><li>Each element in the array appears twice except for one element which appears only once.</li></ul>



<h2><strong>Solution: XOR</strong></h2>



<p>single_number ^ a ^ b ^ c ^ &#8230; ^ a ^ b ^ c &#8230; = single_number</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="crayon-plain-tag">class Solution {
public:
  int singleNumber(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    for (int x : nums)
      ans ^= x;
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/" data-type="post" data-id="8846">花花酱 LeetCode 137. Single Number II</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/">花花酱 LeetCode 136. Single Number</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/bit/leetcode-136-single-number/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 41. First Missing Positive</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 27 Nov 2021 02:40:37 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[marking]]></category>
		<category><![CDATA[missing]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8801</guid>

					<description><![CDATA[<p>Given an unsorted integer array&#160;nums, return the smallest missing positive integer. You must implement an algorithm that runs in&#160;O(n)&#160;time and uses constant extra space. Example&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/">花花酱 LeetCode 41. First Missing Positive</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 unsorted integer array&nbsp;<code>nums</code>, return the smallest missing positive integer.</p>



<p>You must implement an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and uses constant extra space.</p>



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



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



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



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [7,8,9,11,12]
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li><li><code>-2<sup>31</sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li></ul>



<h2><strong>Solution: Marking</strong></h2>



<p>First pass, marking nums[i] to INT_MAX if nums[i] &lt;= 0<br>Second pass, use a negative number to mark the presence of a number x at nums[x &#8211; 1]<br>Third pass, the first positive number is the missing index i, return i +1<br>If not found return n + 1.</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="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int firstMissingPositive(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    for (int&amp; x : nums)
      if (x &lt;= 0) x = INT_MAX;
    
    for (int x : nums) {
      x = abs(x);
      if (x &gt;= 1 &amp;&amp; x &lt;= n &amp;&amp; nums[x - 1] &gt; 0)
        nums[x - 1] *= -1;
    }
    
    for (int i = 0; i &lt; n; ++i)
      if (nums[i] &gt; 0)
        return i + 1;
    
    return n + 1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/">花花酱 LeetCode 41. First Missing Positive</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-41-first-missing-positive/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 448. Find All Numbers Disappeared in an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-448-find-all-numbers-disappeared-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-448-find-all-numbers-disappeared-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 23 Mar 2018 07:33:44 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[missing]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2315</guid>

					<description><![CDATA[<p>Problem Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-448-find-all-numbers-disappeared-in-an-array/">花花酱 LeetCode 448. Find All Numbers Disappeared in an Array</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>
<div class="question-description">
<div>
<p>Given an array of integers where 1 ≤ a[i] ≤ <i>n</i> (<i>n</i> = size of array), some elements appear twice and others appear once.</p>
<p>Find all the elements of [1, <i>n</i>] inclusive that do not appear in this array.</p>
<p>Could you do it without extra space and in O(<i>n</i>) runtime? You may assume the returned list does not count as extra space.</p>
<p><b>Example:</b></p>
<pre class="crayon:false "><b>Input:</b>
[4,3,2,7,8,2,3,1]

<b>Output:</b>
[5,6]
</pre>
</div>
</div>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 128 ms (beats 93.70%)
class Solution {
public:
  vector&lt;int&gt; findDisappearedNumbers(vector&lt;int&gt;&amp; nums) {
    for (size_t i = 0; i != nums.size(); ++i) {
      int index = abs(nums[i]) - 1;
      if (nums[index] &gt; 0) nums[index] *= -1;
    }
    vector&lt;int&gt; ans;
    for (size_t i = 0; i != nums.size(); ++i)
      if (nums[i] &gt; 0) ans.push_back(i + 1);
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-448-find-all-numbers-disappeared-in-an-array/">花花酱 LeetCode 448. Find All Numbers Disappeared in an Array</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-448-find-all-numbers-disappeared-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 268. Missing Number</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-268-missing-number/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-268-missing-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 09 Sep 2017 20:11:14 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[missing]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[sum]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=180</guid>

					<description><![CDATA[<p>Problem: Given an array containing n distinct numbers taken from 0, 1, 2, &#8230;, n, find the one that is missing from the array. For&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-268-missing-number/">花花酱 LeetCode 268. Missing Number</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/z0p_FBatGWM?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given an array containing n distinct numbers taken from 0, 1, 2, &#8230;, n, find the one that is missing from the array.</p>
<p>For example,<br />
Given nums = [0, 1, 3] return 2.</p>
<p>Note:<br />
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?</p>
<p>&nbsp;</p>
<p><strong>Idea:</strong></p>
<p>sum / xor</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/268-ep44.png"><img class="alignnone size-full wp-image-183" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/268-ep44.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/268-ep44.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/268-ep44-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/268-ep44-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/268-ep44-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution:</strong></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    // Solution 1: Sum
    int missingNumber(vector&lt;int&gt;&amp; nums) {
        int n = nums.size();
        int x = (0+n)*(n+1)/2 - accumulate(nums.begin(), nums.end(), 0);
        return x;
    }
    
    // Solution 2: XOR
    int missingNumber(vector&lt;int&gt;&amp; nums) {
        int n = nums.size();
        int x = 0;
        for(int i=1;i&lt;=n;++i)
            x = x ^ i ^ nums[i-1];
        return x;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-268-missing-number/">花花酱 LeetCode 268. Missing Number</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-268-missing-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
