<?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>bit Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/bit/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/bit/</link>
	<description></description>
	<lastBuildDate>Mon, 26 Sep 2022 17:10:19 +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>bit Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/bit/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2419. Longest Subarray With Maximum Bitwise AND</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 26 Sep 2022 17:09:13 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[and]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9845</guid>

					<description><![CDATA[<p>You are given an integer array&#160;nums&#160;of size&#160;n. Consider a&#160;non-empty&#160;subarray from&#160;nums&#160;that has the&#160;maximum&#160;possible&#160;bitwise AND. In other words, let&#160;k&#160;be the maximum value of the bitwise AND of&#160;any&#160;subarray&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/">花花酱 LeetCode 2419. Longest Subarray With Maximum Bitwise AND</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer array&nbsp;<code>nums</code>&nbsp;of size&nbsp;<code>n</code>.</p>



<p>Consider a&nbsp;<strong>non-empty</strong>&nbsp;subarray from&nbsp;<code>nums</code>&nbsp;that has the&nbsp;<strong>maximum</strong>&nbsp;possible&nbsp;<strong>bitwise AND</strong>.</p>



<ul><li>In other words, let&nbsp;<code>k</code>&nbsp;be the maximum value of the bitwise AND of&nbsp;<strong>any</strong>&nbsp;subarray of&nbsp;<code>nums</code>. Then, only subarrays with a bitwise AND equal to&nbsp;<code>k</code>&nbsp;should be considered.</li></ul>



<p>Return&nbsp;<em>the length of the&nbsp;<strong>longest</strong>&nbsp;such subarray</em>.</p>



<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p>



<p>A&nbsp;<strong>subarray</strong>&nbsp;is a contiguous sequence of elements within an array.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,3,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
</pre>



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



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



<h2><strong>Solution: Find the largest number</strong></h2>



<p>a &amp; b &lt;= a<br>a &amp; b &lt;= b<br>if b > a, a &amp; b &lt; b, we choose to start a new sequence of &#8220;b&#8221; instead of continuing with &#8220;ab&#8221;</p>



<p>Basically, we find the largest number in the array and count the longest sequence of it. Note, there will be some tricky cases like.<br>b b b b a b<br>b a b b b b<br>We need to return 4 instead of 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 longestSubarray(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    int best = 0;
    for (int i = 0, l = 0; i &lt; nums.size(); ++i) {
      if (nums[i] &gt; best) {
        best = nums[i]; 
        ans = l = 1;
      } else if (nums[i] == best) {
        ans = max(ans, ++l);
      } else {
        l = 0;
      }
    }    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2419-longest-subarray-with-maximum-bitwise-and/">花花酱 LeetCode 2419. Longest Subarray With Maximum Bitwise AND</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-2419-longest-subarray-with-maximum-bitwise-and/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2317. Maximum XOR After Operations</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-2317-maximum-xor-after-operations/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-2317-maximum-xor-after-operations/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Jun 2022 03:58:32 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9766</guid>

					<description><![CDATA[<p>You are given a 0-indexed integer array nums. In one operation, select any non-negative integer x and an index i, then update nums[i] to be equal to nums[i] AND (nums[i] XOR x). Note that&#160;AND&#160;is the bitwise AND&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-2317-maximum-xor-after-operations/">花花酱 LeetCode 2317. Maximum XOR After Operations</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 is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 2317. Maximum XOR After Operations - 刷题找工作 EP398" width="500" height="281" src="https://www.youtube.com/embed/8wexnTx5wlE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, select <strong>any</strong> non-negative integer <code>x</code> and an index <code>i</code>, then <strong>update</strong> <code>nums[i]</code> to be equal to <code>nums[i] AND (nums[i] XOR x)</code>.</p>



<p>Note that&nbsp;<code>AND</code>&nbsp;is the bitwise AND operation and&nbsp;<code>XOR</code>&nbsp;is the bitwise XOR operation.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>maximum</strong>&nbsp;possible bitwise XOR of all elements of&nbsp;</em><code>nums</code><em>&nbsp;after applying the operation&nbsp;<strong>any number</strong>&nbsp;of times</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,2,4,6]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
It can be shown that 7 is the maximum possible bitwise XOR.
Note that other operations may be used to achieve a bitwise XOR of 7.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,9,2]
<strong>Output:</strong> 11
<strong>Explanation:</strong> Apply the operation zero times.
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
It can be shown that 11 is the maximum possible bitwise XOR.</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= nums[i] &lt;= 10<sup>8</sup></code></li></ul>



<h2><strong>Solution: Bitwise OR</strong></h2>



<p>The maximum possible number MAX = nums[0] | nums[1] | &#8230; | nums[n &#8211; 1]. </p>



<p>We need to prove:<br>1) MAX is achievable.<br>2) MAX is the largest number we can get.</p>



<p><code>nums[i] AND (nums[i] XOR x)</code> means that we can turn any 1 bits to 0 for nums[i].</p>



<p>1) If the i-th bit of MAX is 1, which means there are at least one number with i-th bit equals to 1, however, for XOR, if there are even numbers with i-th bit equal to one, the final results will be 0 for i-th bit, we get a smaller number. By using the operation, we can choose one of them and flip the bit.<br><br>**1** XOR <meta charset="utf-8">**1** XOR <meta charset="utf-8">**1** XOR <meta charset="utf-8">**1** = **0** =&gt; <br>**<strong><span class="has-inline-color has-vivid-red-color">0</span></strong>** XOR <meta charset="utf-8">**1** XOR <meta charset="utf-8">**1** XOR <meta charset="utf-8">**1** = **<strong><span class="has-inline-color has-vivid-red-color">1</span></strong>**<br></p>



<p>2) If the i-th bit of MAX is 0, which means the i-th bit of all the numbers is 0, there is nothing we can do with the operation, and the XOR will be 0 as well.<br>e.g. <meta charset="utf-8">**0** XOR <meta charset="utf-8">**0** XOR <meta charset="utf-8">**0** XOR <meta charset="utf-8">**0** = **0**</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 maximumXOR(vector&lt;int&gt;&amp; nums) {
    return reduce(begin(nums), end(nums), 0, bit_or());
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-2317-maximum-xor-after-operations/">花花酱 LeetCode 2317. Maximum XOR After Operations</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-2317-maximum-xor-after-operations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2220. Minimum Bit Flips to Convert Number</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-2220-minimum-bit-flips-to-convert-number/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-2220-minimum-bit-flips-to-convert-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 02 Apr 2022 21:30:55 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9601</guid>

					<description><![CDATA[<p>A&#160;bit flip&#160;of a number&#160;x&#160;is choosing a bit in the binary representation of&#160;x&#160;and&#160;flipping&#160;it from either&#160;0&#160;to&#160;1&#160;or&#160;1&#160;to&#160;0. For example, for&#160;x = 7, the binary representation is&#160;111&#160;and we may&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-2220-minimum-bit-flips-to-convert-number/">花花酱 LeetCode 2220. Minimum Bit Flips to Convert 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>A&nbsp;<strong>bit flip</strong>&nbsp;of a number&nbsp;<code>x</code>&nbsp;is choosing a bit in the binary representation of&nbsp;<code>x</code>&nbsp;and&nbsp;<strong>flipping</strong>&nbsp;it from either&nbsp;<code>0</code>&nbsp;to&nbsp;<code>1</code>&nbsp;or&nbsp;<code>1</code>&nbsp;to&nbsp;<code>0</code>.</p>



<ul><li>For example, for&nbsp;<code>x = 7</code>, the binary representation is&nbsp;<code>111</code>&nbsp;and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get&nbsp;<code>110</code>, flip the second bit from the right to get&nbsp;<code>101</code>, flip the fifth bit from the right (a leading zero) to get&nbsp;<code>10111</code>, etc.</li></ul>



<p>Given two integers&nbsp;<code>start</code>&nbsp;and&nbsp;<code>goal</code>, return<em>&nbsp;the&nbsp;<strong>minimum</strong>&nbsp;number of&nbsp;<strong>bit flips</strong>&nbsp;to convert&nbsp;</em><code>start</code><em>&nbsp;to&nbsp;</em><code>goal</code>.</p>



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



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> start = 10, goal = 7
<strong>Output:</strong> 3
<strong>Explanation:</strong> The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
- Flip the first bit from the right: 1010 -&gt; 1011.
- Flip the third bit from the right: 1011 -&gt; 1111.
- Flip the fourth bit from the right: 1111 -&gt; 0111.
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.</pre>



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



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> start = 3, goal = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
- Flip the first bit from the right: 011 -&gt; 010.
- Flip the second bit from the right: 010 -&gt; 000.
- Flip the third bit from the right: 000 -&gt; 100.
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
</pre>



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



<ul><li><code>0 &lt;= start, goal &lt;= 10<sup>9</sup></code></li></ul>



<p><strong>Solution: XOR</strong></p>



<p>start ^ goal will give us the bitwise difference of start and goal in binary format. <br>ans = # of 1 ones in the xor-ed results. <br>For C++, we can use __builtin_popcount or bitset&lt;32>::count() to get the number of bits set for a given integer.</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 minBitFlips(int start, int goal) {
    return bitset&lt;32&gt;(start ^ goal).count(); // __builtin_popcount(start ^ goal);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-2220-minimum-bit-flips-to-convert-number/">花花酱 LeetCode 2220. Minimum Bit Flips to Convert 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-2220-minimum-bit-flips-to-convert-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1920. Build Array from Permutation</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1920-build-array-from-permutation/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1920-build-array-from-permutation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Dec 2021 02:20:42 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9234</guid>

					<description><![CDATA[<p>Given a&#160;zero-based permutation&#160;nums&#160;(0-indexed), build an array&#160;ans&#160;of the&#160;same length&#160;where&#160;ans[i] = nums[nums[i]]&#160;for each&#160;0 &#60;= i &#60; nums.length&#160;and return it. A&#160;zero-based permutation&#160;nums&#160;is an array of&#160;distinct&#160;integers from&#160;0&#160;to&#160;nums.length - 1&#160;(inclusive).&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1920-build-array-from-permutation/">花花酱 LeetCode 1920. Build Array from Permutation</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>zero-based permutation</strong>&nbsp;<code>nums</code>&nbsp;(<strong>0-indexed</strong>), build an array&nbsp;<code>ans</code>&nbsp;of the&nbsp;<strong>same length</strong>&nbsp;where&nbsp;<code>ans[i] = nums[nums[i]]</code>&nbsp;for each&nbsp;<code>0 &lt;= i &lt; nums.length</code>&nbsp;and return it.</p>



<p>A&nbsp;<strong>zero-based permutation</strong>&nbsp;<code>nums</code>&nbsp;is an array of&nbsp;<strong>distinct</strong>&nbsp;integers from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>nums.length - 1</code>&nbsp;(<strong>inclusive</strong>).</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,2,1,5,3,4]
<strong>Output:</strong> [0,1,2,4,5,3]<strong>
Explanation:</strong> The array ans is built as follows: 
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
    = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
    = [0,1,2,4,5,3]</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,0,1,2,3,4]
<strong>Output:</strong> [4,5,0,1,2,3]
<strong>Explanation:</strong> The array ans is built as follows:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
    = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
    = [4,5,0,1,2,3]</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>0 &lt;= nums[i] &lt; nums.length</code></li><li>The elements in&nbsp;<code>nums</code>&nbsp;are&nbsp;<strong>distinct</strong>.</li></ul>



<p><strong>Follow-up:</strong>&nbsp;Can you solve it without using an extra space (i.e.,&nbsp;<code>O(1)</code>&nbsp;memory)?</p>



<h2><strong>Solution 1: Straight forward</strong></h2>



<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="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; buildArray(vector&lt;int&gt;&amp; nums) {
    vector&lt;int&gt; ans(nums);
    for (size_t i = 0; i &lt; nums.size(); ++i)
      ans[i] = nums[nums[i]];
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Follow up: Inplace Encoding</strong></h2>



<p>Since nums[i] &lt;= 1000, we can use low 16 bit to store the original value and high 16 bit for new value.</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:
  vector&lt;int&gt; buildArray(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    for (int i = 0; i &lt; n; ++i)
      nums[i] |= (nums[nums[i]] &amp; 0xffff) &lt;&lt; 16;
    for (int i = 0; i &lt; n; ++i)
      nums[i] &gt;&gt;= 16;
    return nums;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1920-build-array-from-permutation/">花花酱 LeetCode 1920. Build Array from Permutation</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-1920-build-array-from-permutation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 231. Power of Two</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-231-power-of-two/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-231-power-of-two/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 08 Dec 2021 05:22:11 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9069</guid>

					<description><![CDATA[<p>Given an integer&#160;n, return&#160;true&#160;if it is a power of two. Otherwise, return&#160;false. An integer&#160;n&#160;is a power of two, if there exists an integer&#160;x&#160;such that&#160;n ==&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-231-power-of-two/">花花酱 LeetCode 231. Power of Two</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an integer&nbsp;<code>n</code>, return&nbsp;<em><code>true</code>&nbsp;if it is a power of two. Otherwise, return&nbsp;<code>false</code></em>.</p>



<p>An integer&nbsp;<code>n</code>&nbsp;is a power of two, if there exists an integer&nbsp;<code>x</code>&nbsp;such that&nbsp;<code>n == 2<sup>x</sup></code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4
<strong>Output:</strong> true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5
<strong>Output:</strong> false
</pre>



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



<ul><li><code>-2<sup>31</sup>&nbsp;&lt;= n &lt;= 2<sup>31</sup>&nbsp;- 1</code></li></ul>



<h2><strong>Solution: 1 bit set</strong></h2>



<p>Any power of two has only 1 bit set in the binary format. e.g. 1=0b01, 2=0b10, 4=0b100, 8=0b1000</p>



<ol><li>use popcount.</li></ol>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool isPowerOfTwo(int n) {
     return n &gt; 0 ? __builtin_popcount(n) == 1 : false;
  }
};</pre>
</div></div>



<p>2. Use (n) &amp; (n &#8211; 1) to remove the last set bit, so it should be zero.</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool isPowerOfTwo(int n) {
     return n &gt; 0 ? !(n &amp; (n - 1)) : false;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-231-power-of-two/">花花酱 LeetCode 231. Power of Two</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-231-power-of-two/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 201. Bitwise AND of Numbers Range</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-201-bitwise-and-of-numbers-range/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-201-bitwise-and-of-numbers-range/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 06 Dec 2021 02:38:22 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9038</guid>

					<description><![CDATA[<p>Given two integers&#160;left&#160;and&#160;right&#160;that represent the range&#160;[left, right], return&#160;the bitwise AND of all numbers in this range, inclusive. Example 1: Input: left = 5, right =&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-201-bitwise-and-of-numbers-range/">花花酱 LeetCode 201. Bitwise AND of Numbers 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 integers&nbsp;<code>left</code>&nbsp;and&nbsp;<code>right</code>&nbsp;that represent the range&nbsp;<code>[left, right]</code>, return&nbsp;<em>the bitwise AND of all numbers in this range, inclusive</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>



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



<ul><li><code>0 &lt;= left &lt;= right &lt;= 2<sup>31</sup>&nbsp;- 1</code></li></ul>



<h2><strong>Solution: Bit operation</strong></h2>



<p>Bitwise AND all the numbers between left and right will clear out all the <strong>low bits</strong>. Basically this question is asking to find the common prefix of left and right in the binary format.</p>



<p><br>5 = 0b0101<br>7 = 0b0111<br>the common prefix is 0b0100 which is 4.</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="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int rangeBitwiseAnd(int left, int right) {
    while (right &gt; left)
      right &amp;= (right - 1); // clears the lowest set bit.
    return right;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-201-bitwise-and-of-numbers-range/">花花酱 LeetCode 201. Bitwise AND of Numbers 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/bit/leetcode-201-bitwise-and-of-numbers-range/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 191. Number of 1 Bits</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-191-number-of-1-bits/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-191-number-of-1-bits/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 29 Nov 2021 03:30:37 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8888</guid>

					<description><![CDATA[<p>Write a function that takes an unsigned integer and returns the number of &#8216;1&#8217; bits it has (also known as the&#160;Hamming weight). Note: Note that&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-191-number-of-1-bits/">花花酱 LeetCode 191. Number of 1 Bits</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>Write a function that takes an unsigned integer and returns the number of &#8216;1&#8217; bits it has (also known as the&nbsp;<a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank" rel="noreferrer noopener">Hamming weight</a>).</p>



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



<ul><li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer&#8217;s internal binary representation is the same, whether it is signed or unsigned.</li><li>In Java, the compiler represents the signed integers using&nbsp;<a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank" rel="noreferrer noopener">2&#8217;s complement notation</a>. Therefore, in&nbsp;<strong>Example 3</strong>, the input represents the signed integer.&nbsp;<code>-3</code>.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 00000000000000000000000000001011
<strong>Output:</strong> 3
<strong>Explanation:</strong> The input binary string <strong>00000000000000000000000000001011</strong> has a total of three '1' bits.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 00000000000000000000000010000000
<strong>Output:</strong> 1
<strong>Explanation:</strong> The input binary string <strong>00000000000000000000000010000000</strong> has a total of one '1' bit.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 11111111111111111111111111111101
<strong>Output:</strong> 31
<strong>Explanation:</strong> The input binary string <strong>11111111111111111111111111111101</strong> has a total of thirty one '1' bits.
</pre>



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



<ul><li>The input must be a&nbsp;<strong>binary string</strong>&nbsp;of length&nbsp;<code>32</code>.</li></ul>



<p><strong>Follow up:</strong>&nbsp;If this function is called many times, how would you optimize it?</p>



<h2><strong>Solution: Bit Operation</strong></h2>



<p>Use n &amp; 1 to get the lowest bit of n. <br>Use n &gt;&gt;= 1 to right shift n for 1 bit, e.g. removing the last bit.</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="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int hammingWeight(uint32_t n) {
    int ans = 0;
    while (n) {
      ans += n &amp; 1;
      n &gt;&gt;= 1;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-191-number-of-1-bits/">花花酱 LeetCode 191. Number of 1 Bits</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-191-number-of-1-bits/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 137. Single Number II</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 05:11:55 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[mask]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8846</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums&#160;where&#160;every element appears&#160;three times&#160;except for one, which appears&#160;exactly once.&#160;Find the single element and return it. You must&#160;implement a solution with a linear&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/">花花酱 LeetCode 137. Single Number II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an integer array&nbsp;<code>nums</code>&nbsp;where&nbsp;every element appears&nbsp;<strong>three times</strong>&nbsp;except for one, which appears&nbsp;<strong>exactly once</strong>.&nbsp;<em>Find the single element and return it</em>.</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,3,2]
<strong>Output:</strong> 3
</pre>



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



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



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



<ul><li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li><li><code>-2<sup>31</sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li><li>Each element in&nbsp;<code>nums</code>&nbsp;appears exactly&nbsp;<strong>three times</strong>&nbsp;except for one element which appears&nbsp;<strong>once</strong>.</li></ul>



<h2><strong>Solution: Bit by bit</strong></h2>



<p>Since every number appears three times, the i-th bit must be a factor of 3, if not, that bit belongs to the single number.</p>



<p>Time complexity: O(32n)<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 singleNumber(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    for (int s = 0; s &lt; 32; ++s) {
      int mask = 1 &lt;&lt; s;
      int sum = 0;
      for (int x : nums)
        if (x &amp; mask) ++sum;
      if (sum % 3) ans |= mask;
    }
    return ans;
  }
};</pre>
</div></div>



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



<ul><li><a href="https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/" data-type="post" data-id="8843">花花酱 LeetCode 136. Single Number</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/">花花酱 LeetCode 137. Single Number II</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-137-single-number-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 29. Divide Two Integers</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-29-divide-two-integers/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-29-divide-two-integers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 26 Nov 2021 21:48:08 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8785</guid>

					<description><![CDATA[<p>Given two integers&#160;dividend&#160;and&#160;divisor, divide two integers&#160;without&#160;using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-29-divide-two-integers/">花花酱 LeetCode 29. Divide Two Integers</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 integers&nbsp;<code>dividend</code>&nbsp;and&nbsp;<code>divisor</code>, divide two integers&nbsp;<strong>without</strong>&nbsp;using multiplication, division, and mod operator.</p>



<p>The integer division should truncate toward zero, which means losing its fractional part. For example,&nbsp;<code>8.345</code>&nbsp;would be truncated to&nbsp;<code>8</code>, and&nbsp;<code>-2.7335</code>&nbsp;would be truncated to&nbsp;<code>-2</code>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>quotient</strong>&nbsp;after dividing&nbsp;</em><code>dividend</code><em>&nbsp;by&nbsp;</em><code>divisor</code>.</p>



<p><strong>Note:&nbsp;</strong>Assume we are dealing with an environment that could only store integers within the&nbsp;<strong>32-bit</strong>&nbsp;signed integer range:&nbsp;<code>[−2<sup>31</sup>, 2<sup>31</sup>&nbsp;− 1]</code>. For this problem, if the quotient is&nbsp;<strong>strictly greater than</strong>&nbsp;<code>2<sup>31</sup>&nbsp;- 1</code>, then return&nbsp;<code>2<sup>31</sup>&nbsp;- 1</code>, and if the quotient is&nbsp;<strong>strictly less than</strong>&nbsp;<code>-2<sup>31</sup></code>, then return&nbsp;<code>-2<sup>31</sup></code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> dividend = 10, divisor = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 10/3 = 3.33333.. which is truncated to 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> dividend = 7, divisor = -3
<strong>Output:</strong> -2
<strong>Explanation:</strong> 7/-3 = -2.33333.. which is truncated to -2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> dividend = 0, divisor = 1
<strong>Output:</strong> 0
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> dividend = 1, divisor = 1
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>-2<sup>31</sup>&nbsp;&lt;= dividend, divisor &lt;= 2<sup>31</sup>&nbsp;- 1</code></li><li><code>divisor != 0</code></li></ul>



<h2><strong>Solution: Binary / Bit Operation</strong></h2>



<p>The answer can be represented in binary format.</p>



<p>a / b = c<br>a >= b *  Σ(d<sub>i</sub>*2<sup>i</sup>)<br>c = Σ(d<sub>i</sub>*2<sup>i</sup>)</p>



<p>e.g. 1: 10 / 3<br>=> 10 >= 3*(2<sup>1</sup> + 2<sup>0</sup>) = 3 * (2 + 1) = 9<br>ans = 2<sup>1</sup> + 2<sup>0</sup> = 3<br><br>e.g. 2: 100 / 7<br>=> 100 >= 7*(2<sup>3</sup> + 2<sup>2</sup>+2<sup>1</sup>) = 7 * (8 + 4 + 2) = 7 * 14 = 98<br>ans = 2<sup>3</sup>+2<sup>2</sup>+2<sup>1</sup>=8+4+2=14</p>



<p>Time complexity: O(32)<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 divide(int A, int B) {
    if (B == INT_MIN)
      return A == INT_MIN ? 1 : 0;
    if (A == INT_MIN) {
      if (B == -1) return INT_MAX;
      return B &gt; 0 ? divide(A + B, B) - 1 : divide(A - B, B) + 1;
    }    
    int a = abs(A);
    int b = abs(B);
    int ans = 0;
    for (int x = 31; x &gt;= 0; --x)
      if (a &gt;&gt; x &gt;= b)
        ans += 1 &lt;&lt; x, a -= b &lt;&lt; x;
    return (A &gt; 0) == (B &gt; 0) ? ans : -ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-29-divide-two-integers/">花花酱 LeetCode 29. Divide Two Integers</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-29-divide-two-integers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2032. Two Out of Three</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 22 Oct 2021 03:55:16 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashmap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8627</guid>

					<description><![CDATA[<p>Given three integer arrays&#160;nums1,&#160;nums2, and&#160;nums3, return&#160;a&#160;distinct&#160;array containing all the values that are present in&#160;at least two&#160;out of the three arrays. You may return the values&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/">花花酱 LeetCode 2032. Two Out of Three</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 three integer arrays&nbsp;<code>nums1</code>,&nbsp;<code>nums2</code>, and&nbsp;<code>nums3</code>, return&nbsp;<em>a&nbsp;<strong>distinct</strong>&nbsp;array containing all the values that are present in&nbsp;<strong>at least two</strong>&nbsp;out of the three arrays. You may return the values in&nbsp;<strong>any</strong>&nbsp;order</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
<strong>Output:</strong> [3,2]
<strong>Explanation:</strong> The values that are present in at least two arrays are:
- 3, in all three arrays.
- 2, in nums1 and nums2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
<strong>Output:</strong> [2,3,1]
<strong>Explanation:</strong> The values that are present in at least two arrays are:
- 2, in nums2 and nums3.
- 3, in nums1 and nums2.
- 1, in nums1 and nums3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
<strong>Output:</strong> []
<strong>Explanation:</strong> No value is present in at least two arrays.
</pre>



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



<ul><li><code>1 &lt;= nums1.length, nums2.length, nums3.length &lt;= 100</code></li><li><code>1 &lt;= nums1[i], nums2[j], nums3[k] &lt;= 100</code></li></ul>



<h2><strong>Solution: Hashmap / Bitmask</strong></h2>



<p>s[x] := bitmask of x in all array[i]</p>



<p>s[x] = 101 =&gt; x in array0 and array2</p>



<p>Time complexity: O(n1 + n2 + n3)<br>Space complexity: O(n1 + n2 + n3)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; twoOutOfThree(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2, vector&lt;int&gt;&amp; nums3) {
    unordered_map&lt;int, int&gt; s;    
    for (int x : nums1) s[x] |= 1;
    for (int x : nums2) s[x] |= 2;
    for (int x : nums3) s[x] |= 4;
    vector&lt;int&gt; ans;
    for (auto [x, v] : s)
      if (__builtin_popcount(v) &gt;= 2) ans.push_back(x);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/">花花酱 LeetCode 2032. Two Out of Three</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/hashtable/leetcode-2032-two-out-of-three/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1835. Find XOR Sum of All Pairs Bitwise AND</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1835-find-xor-sum-of-all-pairs-bitwise-and/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1835-find-xor-sum-of-all-pairs-bitwise-and/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 18 Apr 2021 17:47:53 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8379</guid>

					<description><![CDATA[<p>The&#160;XOR sum&#160;of a list is the bitwise&#160;XOR&#160;of all its elements. If the list only contains one element, then its&#160;XOR sum&#160;will be equal to this element.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1835-find-xor-sum-of-all-pairs-bitwise-and/">花花酱 LeetCode 1835. Find XOR Sum of All Pairs Bitwise AND</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>The&nbsp;<strong>XOR sum</strong>&nbsp;of a list is the bitwise&nbsp;<code>XOR</code>&nbsp;of all its elements. If the list only contains one element, then its&nbsp;<strong>XOR sum</strong>&nbsp;will be equal to this element.</p>



<ul><li>For example, the&nbsp;<strong>XOR sum</strong>&nbsp;of&nbsp;<code>[1,2,3,4]</code>&nbsp;is equal to&nbsp;<code>1 XOR 2 XOR 3 XOR 4 = 4</code>, and the&nbsp;<strong>XOR sum</strong>&nbsp;of&nbsp;<code>[3]</code>&nbsp;is equal to&nbsp;<code>3</code>.</li></ul>



<p>You are given two&nbsp;<strong>0-indexed</strong>&nbsp;arrays&nbsp;<code>arr1</code>&nbsp;and&nbsp;<code>arr2</code>&nbsp;that consist only of non-negative integers.</p>



<p>Consider the list containing the result of&nbsp;<code>arr1[i] AND arr2[j]</code>&nbsp;(bitwise&nbsp;<code>AND</code>) for every&nbsp;<code>(i, j)</code>&nbsp;pair where&nbsp;<code>0 &lt;= i &lt; arr1.length</code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; arr2.length</code>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>XOR sum</strong>&nbsp;of the aforementioned list</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr1 = [1,2,3], arr2 = [6,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The list = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1].
The XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr1 = [12], arr2 = [4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The list = [12 AND 4] = [4]. The XOR sum = 4.
</pre>



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



<ul><li><code>1 &lt;= arr1.length, arr2.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= arr1[i], arr2[j] &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Bit </strong></h2>



<p>(a[0] &amp; b[i]) ^ (a[1] &amp; b[i]) ^ &#8230; ^ (a[n-1] &amp; b[i]) = (a[0] ^ a[1] ^ &#8230;  ^ a[n-1]) &amp; b[i]</p>



<p>We can pre compute that xor sum of array A.</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 getXORSum(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    int ans = 0;    
    int xora = 0;
    for (int a : A) xora ^= a;    
    for (int b : B) ans ^= (xora &amp; b);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1835-find-xor-sum-of-all-pairs-bitwise-and/">花花酱 LeetCode 1835. Find XOR Sum of All Pairs Bitwise AND</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-1835-find-xor-sum-of-all-pairs-bitwise-and/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1829. Maximum XOR for Each Query</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1829-maximum-xor-for-each-query/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1829-maximum-xor-for-each-query/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Apr 2021 19:08:24 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8361</guid>

					<description><![CDATA[<p>You are given a&#160;sorted&#160;array&#160;nums&#160;of&#160;n&#160;non-negative integers and an integer&#160;maximumBit. You want to perform the following query&#160;n&#160;times: Find a non-negative integer&#160;k &#60; 2maximumBit&#160;such that&#160;nums[0] XOR nums[1] XOR&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1829-maximum-xor-for-each-query/">花花酱 LeetCode 1829. Maximum XOR for Each Query</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>sorted</strong>&nbsp;array&nbsp;<code>nums</code>&nbsp;of&nbsp;<code>n</code>&nbsp;non-negative integers and an integer&nbsp;<code>maximumBit</code>. You want to perform the following query&nbsp;<code>n</code>&nbsp;<strong>times</strong>:</p>



<ol><li>Find a non-negative integer&nbsp;<code>k &lt; 2<sup>maximumBit</sup></code>&nbsp;such that&nbsp;<code>nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k</code>&nbsp;is&nbsp;<strong>maximized</strong>.&nbsp;<code>k</code>&nbsp;is the answer to the&nbsp;<code>i<sup>th</sup></code>&nbsp;query.</li><li>Remove the&nbsp;<strong>last&nbsp;</strong>element from the current array&nbsp;<code>nums</code>.</li></ol>



<p>Return&nbsp;<em>an array</em>&nbsp;<code>answer</code><em>, where&nbsp;</em><code>answer[i]</code><em>&nbsp;is the answer to the&nbsp;</em><code>i<sup>th</sup></code><em>&nbsp;query</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,1,1,3], maximumBit = 2
<strong>Output:</strong> [0,3,2,3]
<strong>Explanation</strong>: The queries are answered as follows:
1<sup>st</sup> query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.
2<sup>nd</sup> query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.
3<sup>rd</sup> query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.
4<sup>th</sup> query: nums = [0], k = 3 since 0 XOR 3 = 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,4,7], maximumBit = 3
<strong>Output:</strong> [5,2,6,5]
<strong>Explanation</strong>: The queries are answered as follows:
1<sup>st</sup> query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.
2<sup>nd</sup> query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.
3<sup>rd</sup> query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.
4<sup>th</sup> query: nums = [2], k = 5 since 2 XOR 5 = 7.
</pre>



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



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



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



<ul><li><code>nums.length == n</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= maximumBit &lt;= 20</code></li><li><code>0 &lt;= nums[i] &lt; 2<sup>maximumBit</sup></code></li><li><code>nums</code>​​​ is sorted in&nbsp;<strong>ascending</strong>&nbsp;order.</li></ul>



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



<p>Compute s = nums[0] ^ nums[1] ^ &#8230; nums[n-1] first</p>



<p>to remove nums[i], we just need to do s ^= nums[i]</p>



<p>We can always maximize the xor of s and k to (2^maxbit &#8211; 1)<br>k = (2 ^ maxbit &#8211; 1) ^ s</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:
  vector&lt;int&gt; getMaximumXor(vector&lt;int&gt;&amp; nums, int maximumBit) {
    const int t = (1 &lt;&lt; maximumBit) - 1;
    const int n = nums.size();
    vector&lt;int&gt; ans(n);
    int s = 0;
    for (int x : nums) s ^= x;    
    for (int i = n - 1; i &gt;= 0; --i) {
      ans[n - i - 1] = t ^ s;
      s ^= nums[i];
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1829-maximum-xor-for-each-query/">花花酱 LeetCode 1829. Maximum XOR for Each Query</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-1829-maximum-xor-for-each-query/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1734. Decode XORed Permutation</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1734-decode-xored-permutation/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1734-decode-xored-permutation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 23 Jan 2021 22:08:09 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8011</guid>

					<description><![CDATA[<p>There is an integer array&#160;perm&#160;that is a permutation of the first&#160;n&#160;positive integers, where&#160;n&#160;is always&#160;odd. It was encoded into another integer array&#160;encoded&#160;of length&#160;n - 1, such&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1734-decode-xored-permutation/">花花酱 LeetCode 1734. Decode XORed Permutation</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>There is an integer array&nbsp;<code>perm</code>&nbsp;that is a permutation of the first&nbsp;<code>n</code>&nbsp;positive integers, where&nbsp;<code>n</code>&nbsp;is always&nbsp;<strong>odd</strong>.</p>



<p>It was encoded into another integer array&nbsp;<code>encoded</code>&nbsp;of length&nbsp;<code>n - 1</code>, such that&nbsp;<code>encoded[i] = perm[i] XOR perm[i + 1]</code>. For example, if&nbsp;<code>perm = [1,3,2]</code>, then&nbsp;<code>encoded = [2,1]</code>.</p>



<p>Given the&nbsp;<code>encoded</code>&nbsp;array, return&nbsp;<em>the original array</em>&nbsp;<code>perm</code>. It is guaranteed that the answer exists and is unique.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> encoded = [3,1]
<strong>Output:</strong> [1,2,3]
<strong>Explanation:</strong> If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]
</pre>



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



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



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



<ul><li><code>3 &lt;= n &lt;&nbsp;10<sup>5</sup></code></li><li><code>n</code>&nbsp;is odd.</li><li><code>encoded.length == n - 1</code></li></ul>



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



<p>The key is to find p[0]. p[i] = p[i &#8211; 1] ^ encoded[i &#8211; 1]</p>



<ol><li>p[0] ^ p[1] ^ &#8230; ^ p[n-1] = 1 ^ 2 ^ &#8230; ^ n</li><li>encoded[1] ^ encode[3] ^ &#8230; ^ encoded[n-2] = (p[1] ^ p[2]) ^ (p[3] ^ p[4]) ^ &#8230; ^ (p[n-2] ^ p[n-1])</li></ol>



<p>1) xor 2) = p[0]</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:
  vector&lt;int&gt; decode(vector&lt;int&gt;&amp; encoded) {    
    const int n = encoded.size() + 1;
    vector&lt;int&gt; perm(n);
    // p[0] = (p[0]^p[1]^...^p[n-1] = 1^2^...^n) 
    //      ^ (p[1]^p[2]^...^p[n-1])
    for (int i = 1; i &lt;= n; ++i) 
      perm[0] ^= i;
    for (int i = 1; i &lt; n; i += 2)
      perm[0] ^= encoded[i];
    for (int i = 1; i &lt; n; ++i)
      perm[i] = perm[i - 1] ^ encoded[i - 1];
    return perm;        
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1734-decode-xored-permutation/">花花酱 LeetCode 1734. Decode XORed Permutation</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-1734-decode-xored-permutation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1720. Decode XORed Array</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1720-decode-xored-array/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1720-decode-xored-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Jan 2021 17:47:03 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7961</guid>

					<description><![CDATA[<p>There is a&#160;hidden&#160;integer array&#160;arr&#160;that consists of&#160;n&#160;non-negative integers. It was encoded into another integer array&#160;encoded&#160;of length&#160;n - 1, such that&#160;encoded[i] = arr[i] XOR arr[i + 1].&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1720-decode-xored-array/">花花酱 LeetCode 1720. Decode XORed 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[
<p>There is a&nbsp;<strong>hidden</strong>&nbsp;integer array&nbsp;<code>arr</code>&nbsp;that consists of&nbsp;<code>n</code>&nbsp;non-negative integers.</p>



<p>It was encoded into another integer array&nbsp;<code>encoded</code>&nbsp;of length&nbsp;<code>n - 1</code>, such that&nbsp;<code>encoded[i] = arr[i] XOR arr[i + 1]</code>. For example, if&nbsp;<code>arr = [1,0,2,1]</code>, then&nbsp;<code>encoded = [1,2,3]</code>.</p>



<p>You are given the&nbsp;<code>encoded</code>&nbsp;array. You are also given an integer&nbsp;<code>first</code>, that is the first element of&nbsp;<code>arr</code>, i.e.&nbsp;<code>arr[0]</code>.</p>



<p>Return&nbsp;<em>the original array</em>&nbsp;<code>arr</code>. It can be proved that the answer exists and is unique.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> encoded = [1,2,3], first = 1
<strong>Output:</strong> [1,0,2,1]
<strong>Explanation:</strong> If arr = [1,0,2,1], then first = 1 and encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> encoded = [6,2,7,3], first = 4
<strong>Output:</strong> [4,2,0,7,4]
</pre>



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



<ul><li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li><li><code>encoded.length == n - 1</code></li><li><code>0 &lt;= encoded[i] &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= first &lt;= 10<sup>5</sup></code></li></ul>



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



<p>encoded[i] = arr[i] ^ arr[i + 1]<br>encoded[i] ^ arr[i] = arr[i] ^ arr[i] ^ arr[i + 1]<br>arr[i+1] = encoded[i]^arr[i]</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="crayon-plain-tag">class Solution {
public:
  vector&lt;int&gt; decode(vector&lt;int&gt;&amp; encoded, int first) {
    const int n = encoded.size() + 1;
    vector&lt;int&gt; ans(n, first);
    for (int i = 0; i + 1 &lt; n; ++i)
      ans[i + 1] = ans[i] ^ encoded[i];
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1720-decode-xored-array/">花花酱 LeetCode 1720. Decode XORed 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/bit/leetcode-1720-decode-xored-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
