|
4 | 4 | <description><![CDATA[My blog, projects, etc.]]></description> |
5 | 5 | <link>https://rivea0.github.io</link> |
6 | 6 | <generator>RSS for Node</generator> |
7 | | - <lastBuildDate>Mon, 23 Dec 2024 12:16:14 GMT</lastBuildDate> |
| 7 | + <lastBuildDate>Tue, 24 Dec 2024 13:24:18 GMT</lastBuildDate> |
8 | 8 | <atom:link href="https://rivea0.github.io/feed.xml" rel="self" type="application/rss+xml"/> |
9 | 9 | <language><![CDATA[en]]></language> |
| 10 | + <item> |
| 11 | + <title><![CDATA[LeetCode Meditations: Missing Number]]></title> |
| 12 | + <description><![CDATA[<p><img src="https://raw.githubusercontent.com/rivea0/leetcode-meditations-assets/main/src/2024-12-24/76-lm.png" alt="Cover image"></p> |
| 13 | +<p>Let's start with the description for <a href="https://leetcode.com/problems/missing-number" target="_blank" rel="noopener noreferrer">this problem</a>:</p> |
| 14 | +<blockquote> |
| 15 | +<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> |
| 16 | +</blockquote> |
| 17 | +<p>For example:</p> |
| 18 | +<pre><code>Input: nums = [3, 0, 1] |
| 19 | +Output: 2 |
| 20 | + |
| 21 | +Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0, 3]. 2 is the missing number in the range since it does not appear in nums. |
| 22 | +</code></pre> |
| 23 | +<p>Or:</p> |
| 24 | +<pre><code>Input: nums = [0, 1] |
| 25 | +Output: 2 |
| 26 | + |
| 27 | +Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0, 2]. 2 is the missing number in the range since it does not appear in nums. |
| 28 | +</code></pre> |
| 29 | +<p>Or:</p> |
| 30 | +<pre><code>Input: nums = [9, 6, 4, 2, 3, 5, 7, 0, 1] |
| 31 | +Output: 8 |
| 32 | + |
| 33 | +Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0, 9]. 8 is the missing number in the range since it does not appear in nums. |
| 34 | +</code></pre> |
| 35 | +<p>It's also stated that <em>all the numbers of <code>nums</code> are <strong>unique</strong>.</em></p> |
| 36 | +<hr> |
| 37 | +<p>One easy way to solve this is to get the total sum of the range, then subtract the sum of the given array. What is left will be the missing number.</p> |
| 38 | +<p>It can be done using <code>reduce</code> to sum the numbers, like this:</p> |
| 39 | +<pre><code class="language-ts">function missingNumber(nums: number[]): number { |
| 40 | + return Array.from({ |
| 41 | + length: nums.length + 1 |
| 42 | + }, (_, idx) => idx).reduce((acc, item) => acc + item, 0) |
| 43 | + - nums.reduce((acc, item) => acc + item, 0); |
| 44 | +} |
| 45 | +</code></pre> |
| 46 | +<p>First, we create an array with values from <code>0</code> to <code>nums.length + 1</code> and get its sum, then subtract the sum of <code>nums</code> from it.</p> |
| 47 | +<p>However, the time and space complexities will be $O(n)$ with this solution as we create an array for the range.</p> |
| 48 | +<p>We can have a more (<em>storage-wise</em>) efficient solution using bit manipulation.<br>In fact, we can use an <a href="https://rivea0.github.io/blog/leetcode-meditations-chapter-14-bit-manipulation#bitwise-xor" target="_blank" rel="noopener noreferrer">XOR</a> operation to help us with that.</p> |
| 49 | +<p>To remember, XOR results in 1 if both of the bits are different — that is, one of them is 0 and the other is 1.<br>When we XOR a number with itself, it will result in 0, as all the bits are the same.</p> |
| 50 | +<p>For example, 3 in binary is <code>11</code>, when we do <code>11 ^ 11</code> the result is <code>0</code>:</p> |
| 51 | +<pre><code class="language-ts">const n = 3; |
| 52 | +const result = n ^ n; // 0 |
| 53 | +</code></pre> |
| 54 | +<p>In other words, <mark>an XOR operation of a number with itself will result in 0</mark>.</p> |
| 55 | +<p>If we do XOR with each number in the array with the indices, eventually all of them will cancel out (result in 0), leaving only the missing number.</p> |
| 56 | +<p>You might think that not all numbers are at their index, for example if <code>nums</code> is <code>[3, 0, 1]</code>, it is obvious that <code>3</code> does not even have an "index 3" that can be associated with it!</p> |
| 57 | +<p>For that, we can start by initializing our result to <code>nums.length</code>. Now, even if the missing number is equal to <code>nums.length</code>, we are handling that edge case.</p> |
| 58 | +<pre><code class="language-ts">let result = nums.length; |
| 59 | +</code></pre> |
| 60 | +<p>Also, <em>XOR is commutative and associative</em>, so it's not important at which index a number appears (or doesn't have one, like in the example above) — they eventually will cancel out.</p> |
| 61 | +<p>Now, with a <code>for</code> loop, we can do the XOR using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR_assignment" target="_blank" rel="noopener noreferrer">bitwise XOR assignment operator</a>:</p> |
| 62 | +<pre><code class="language-ts">for (let i = 0; i < nums.length; i++) { |
| 63 | + result ^= i ^ nums[i]; |
| 64 | +} |
| 65 | +</code></pre> |
| 66 | +<p>And, the final result is the missing number. The solution overall looks like this:</p> |
| 67 | +<pre><code class="language-ts">function missingNumber(nums: number[]): number { |
| 68 | + let result = nums.length; |
| 69 | + for (let i = 0; i < nums.length; i++) { |
| 70 | + result ^= i ^ nums[i]; |
| 71 | + } |
| 72 | + |
| 73 | + return result; |
| 74 | +} |
| 75 | +</code></pre> |
| 76 | +<h4>Time and space complexity</h4> |
| 77 | +<p>The time complexity is again $O(n)$ as we iterate through each number in the array, but the space complexity will be $O(1)$ as we don't have any additional storage need that will grow as the input size increases.</p> |
| 78 | +<hr> |
| 79 | +<p>Next up, we will take a look at the final problem of the entire series, <a href="https://leetcode.com/problems/sum-of-two-integers" target="_blank" rel="noopener noreferrer">Sum of Two Integers</a>. Until then, happy coding.</p> |
| 80 | +]]></description> |
| 81 | + <link>https://rivea0.github.io/blog/leetcode-meditations-missing-number</link> |
| 82 | + <guid isPermaLink="false">https://rivea0.github.io/blog/leetcode-meditations-missing-number</guid> |
| 83 | + <dc:creator><![CDATA[Eda Eren]]></dc:creator> |
| 84 | + <pubDate>Tue, 24 Dec 2024 00:00:00 GMT</pubDate> |
| 85 | + </item> |
10 | 86 | <item> |
11 | 87 | <title><![CDATA[LeetCode Meditations: Reverse Bits]]></title> |
12 | 88 | <description><![CDATA[<p><img src="https://raw.githubusercontent.com/rivea0/leetcode-meditations-assets/main/src/2024-12-23/75-lm.png" alt="Cover image"></p> |
|
0 commit comments