<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bruce Lin]]></title><description><![CDATA[Bruce Lin]]></description><link>https://blog.brucelinweb.com</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Apr 2026 13:50:10 GMT</lastBuildDate><atom:link href="https://blog.brucelinweb.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[LeetCode 153. Find Minimum in Rotated Sorted Array]]></title><description><![CDATA[Question
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

[4,5,6,7,0,1,2] if it was rotated 4 times.

[0,1,2,4,5,6,7] if it was rotated 7 times.


No...]]></description><link>https://blog.brucelinweb.com/leetcode-153-find-minimum-in-rotated-sorted-array</link><guid isPermaLink="true">https://blog.brucelinweb.com/leetcode-153-find-minimum-in-rotated-sorted-array</guid><category><![CDATA[leetcode]]></category><category><![CDATA[coding]]></category><category><![CDATA[C++]]></category><dc:creator><![CDATA[Bruce Lin]]></dc:creator><pubDate>Sun, 29 Jun 2025 08:12:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/ieic5Tq8YMk/upload/01bde166d5fa093873ac43a4383cbbc7.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-question"><strong>Question</strong></h3>
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,2,4,5,6,7]</code> might become:</p>
<ul>
<li><p><code>[4,5,6,7,0,1,2]</code> if it was rotated <code>4</code> times.</p>
</li>
<li><p><code>[0,1,2,4,5,6,7]</code> if it was rotated <code>7</code> times.</p>
</li>
</ul>
<p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p>
<p>Given the sorted rotated array <code>nums</code> of <strong>unique</strong> elements, return <em>the minimum element of this array</em>.</p>
<p>You must write an algorithm that runs in <code>O(log n) time</code>.</p>
<h3 id="heading-example"><strong>Example</strong></h3>
<ul>
<li><p>Input: nums = [3,4,5,1,2]</p>
</li>
<li><p>Output: 1</p>
</li>
<li><p>Explanation: The original array was [1,2,3,4,5] rotated 3 times.</p>
</li>
</ul>
<pre><code class="lang-cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> {</span>
<span class="hljs-keyword">public</span>:
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">findMin</span><span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt;&amp; nums)</span> </span>{
        <span class="hljs-keyword">int</span> left = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">int</span> right = nums.size() - <span class="hljs-number">1</span>;
        <span class="hljs-keyword">int</span> mid;

        <span class="hljs-keyword">while</span> (left &lt; right) {
            mid = left + (right - left) / <span class="hljs-number">2</span>;

            <span class="hljs-keyword">if</span> (nums[mid] &gt; nums[right]) {
                left = mid + <span class="hljs-number">1</span>;
            }
            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (nums[mid] &lt;= nums[right]) {
                right = mid;
            }
        }
        <span class="hljs-keyword">return</span> nums[left];
    }
};
</code></pre>
<h3 id="heading-problem-summary"><strong>Problem Summary</strong></h3>
<p>Given a sorted array of a unique elements that has been rotated, find the minimum element in the array. The algorithm must have a time complexity of O(logn).</p>
<h3 id="heading-core-idea"><strong>Core Idea</strong></h3>
<p>The key constraints is the O(logn) time complexity, which stongly suggests a Binary Search approach.</p>
<p>Although the entire array is not sorted, it is composed of two sorted subarrays. The minimum element is the “pivot” point where the rotation occurs (i.e., the first element of the second sorted array.)</p>
<p>The strategy is to use binary search to efficiently find this pivot point. We can determine which of the two sorted subarrays our <code>mid</code> pointer is in by comparing <code>nums[mid]</code> with the element at the right boundary, <code>nums[right]</code> .</p>
<h3 id="heading-algorithm-logic"><strong>Algorithm Logic</strong></h3>
<ol>
<li><p>Initialize two pinters: <code>left = 0</code> and <code>right = num.size() - 1</code> .</p>
</li>
<li><p>Loop as long as <code>left &lt; right</code>.</p>
</li>
<li><p>Calculate middle index using <code>left + (right - left) / 2</code> . This formula is preferred over <code>(left + right) / 2</code> to prevent potential integer overflow if the numbers are very large.</p>
</li>
<li><p>Compare <code>num[mid]</code> with <code>nums[right]</code> to decide which half of the array to discard:</p>
<ul>
<li><p>If <code>nums[mid] &gt; nums[right]</code> : This indicates that the <code>mid</code> element is in the larger, left sorted subarray. Therefore, the pivot must be in the right half. We discard the left half by setting <code>left = mid + 1</code> .</p>
</li>
<li><p>If <code>num[mid] ≤ nums[right]</code> : This indicates that the segment from <code>mid</code> to <code>right</code> is sorted. The minimum element must be in the left half, and it’s possible that <code>nums[mid]</code> is the minimum itself. We discard the right half by setting <code>right = mid</code> .</p>
</li>
</ul>
</li>
<li><p>The loop terminates when <code>left</code> and <code>right</code> converge to the same index. This index holds the minimum element in the array.</p>
</li>
<li><p>Return <code>nums[left]</code></p>
</li>
<li><p>if nums[mid] &gt; nums[right], then make the left flag be mid + 1.</p>
</li>
<li><p>else if nums[mid] ≤ nums[right], make the right flag be mid.</p>
</li>
</ol>
<h3 id="heading-example-walkthrough"><strong>Example Walkthrough</strong></h3>
<p>Let's trace the algorithm with <code>nums = [3, 4, 5, 1, 2]</code>.</p>
<ul>
<li><p><strong>Initial State:</strong> <code>left = 0</code>, <code>right = 4</code></p>
</li>
<li><p><strong>Iteration 1:</strong></p>
<ul>
<li><p><code>mid = 0 + (4 - 0) / 2 = 2</code></p>
</li>
<li><p><code>nums[mid]</code> is <code>nums[2]</code>, which is <code>5</code>.</p>
</li>
<li><p><code>nums[right]</code> is <code>nums[4]</code>, which is <code>2</code>.</p>
</li>
<li><p>Condition <code>nums[mid] &gt; nums[right]</code> (5 &gt; 2) is <strong>true</strong>.</p>
</li>
<li><p>The minimum must be to the right of <code>mid</code>. We update <code>left = mid + 1 = 3</code>.</p>
</li>
<li><p>New search space: <code>[3, 4]</code> (elements <code>[1, 2]</code>).</p>
</li>
</ul>
</li>
<li><p><strong>Iteration 2:</strong></p>
<ul>
<li><p><code>left = 3</code>, <code>right = 4</code>.</p>
</li>
<li><p><code>mid = 3 + (4 - 3) / 2 = 3</code>.</p>
</li>
<li><p><code>nums[mid]</code> is <code>nums[3]</code>, which is <code>1</code>.</p>
</li>
<li><p><code>nums[right]</code> is <code>nums[4]</code>, which is <code>2</code>.</p>
</li>
<li><p>Condition <code>nums[mid] &gt; nums[right]</code> (1 &gt; 2) is <strong>false</strong>. We use the <code>else</code> block.</p>
</li>
<li><p>The minimum is in the left half (or <code>mid</code> is the minimum). We update <code>right = mid = 3</code>.</p>
</li>
<li><p>New search space: <code>[3, 3]</code> (element <code>[1]</code>).</p>
</li>
</ul>
</li>
<li><p><strong>Termination:</strong></p>
<ul>
<li>Now, <code>left = 3</code> and <code>right = 3</code>. The <code>while (left &lt; right)</code> condition is false. The loop ends.</li>
</ul>
</li>
<li><p><strong>Result:</strong></p>
<ul>
<li>Return <code>nums[left]</code>, which is <code>nums[3]</code>, giving us the answer <code>1</code>.</li>
</ul>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[LeetCode 268: Missing Number]]></title><description><![CDATA[class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int result = nums.size();

        for (int i = 0; i < nums.size(); i++) {
            result ^= i ^ nums[i];
        }

        return result;
    }
};

Question
Given an ar...]]></description><link>https://blog.brucelinweb.com/leetcode-268-missing-number</link><guid isPermaLink="true">https://blog.brucelinweb.com/leetcode-268-missing-number</guid><category><![CDATA[leetcode]]></category><category><![CDATA[coding]]></category><category><![CDATA[C++]]></category><dc:creator><![CDATA[Bruce Lin]]></dc:creator><pubDate>Fri, 27 Jun 2025 07:57:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/ieic5Tq8YMk/upload/cec019be1b80b48cd9976d995cdfd4b1.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<pre><code class="lang-cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> {</span>
<span class="hljs-keyword">public</span>:
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">missingNumber</span><span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt;&amp; nums)</span> </span>{
        <span class="hljs-keyword">int</span> result = nums.size();

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; nums.size(); i++) {
            result ^= i ^ nums[i];
        }

        <span class="hljs-keyword">return</span> result;
    }
};
</code></pre>
<h2 id="heading-question">Question</h2>
<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>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [3,0,1]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p><code>n = 3</code> since there are 3 numbers, so all numbers are in the range <code>[0,3]</code>. 2 is the missing number in the range since it does not appear in <code>nums</code>.</p>
<h2 id="heading-thought">Thought</h2>
<p>This question asks us to find the only missing number, so we can use the XOR <code>^</code> bit operation. It means that <code>0 ^ x = x, x ^ x = 0</code>. Take example 1, for instance, the input nums are 3, 0, 1, and the full numbers are 0, 1, 2, 3.</p>
<p>Therefore, we can XOR all of them: 3^0^1^0^1^2^3 = (3^3) ^ (2) ^ (1^1) ^ (0^0) = 2. As we can see, the answer is 2.</p>
<h2 id="heading-core-idea">Core Idea</h2>
<p>The problem gives us two sets of numbers: the complete range <code>[0, n]</code> and the incomplete input <code>nums</code>. Since only one number is missing, XORing all elements from both sets together will cause every number that appears in both sets to cancel itself out, leaving only the missing number.</p>
<h2 id="heading-method-1-bit-manipulation-xor">Method 1: Bit Manipulation (XOR)</h2>
<h3 id="heading-key-properties">Key Properties</h3>
<ul>
<li><p><code>x ^ x = 0</code></p>
</li>
<li><p><code>x ^ 0 = x</code></p>
</li>
</ul>
<h3 id="heading-logic-walkthrough">Logic Walkthrough</h3>
<ul>
<li><p>Example: nums = <code>[3, 0, 1]</code></p>
</li>
<li><p>n: <code>nums.size()</code> is 3</p>
</li>
<li><p>Complete Range: <code>{0, 1, 2, 3}</code></p>
</li>
<li><p>Input nums: <code>{3, 0, 1}</code></p>
</li>
<li><p>Combined XOR: <code>(0 ^ 1 ^ 2 ^ 3) ^ (3 ^ 0 ^ 1)</code></p>
</li>
<li><p>Rearrange &amp; Cancel: <code>(3 ^ 3) ^ (2) ^ (1 ^ 1) ^ (0 ^ 0) = 2</code></p>
</li>
<li><p>The result is the missing number, <code>2</code>.</p>
</li>
</ul>
<h3 id="heading-complexity">Complexity</h3>
<ul>
<li><p>Time: O(n)</p>
</li>
<li><p>Space: O(1)</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[LeetCode 338: Counting Bits]]></title><description><![CDATA[Problem Overview
Given an integer n, return an array ans of length n + 1 where ans[i] is the number of set bits in the binary representation of i.

Example: n = 5 → Output: [0, 1, 1, 2, 1, 2]

Constraints: 0 <= n <= 10^5


Solution 1: Brute Force
Thi...]]></description><link>https://blog.brucelinweb.com/leetcode-338-counting-bits</link><guid isPermaLink="true">https://blog.brucelinweb.com/leetcode-338-counting-bits</guid><category><![CDATA[leetcode]]></category><category><![CDATA[coding]]></category><category><![CDATA[C++]]></category><dc:creator><![CDATA[Bruce Lin]]></dc:creator><pubDate>Thu, 26 Jun 2025 02:43:01 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/ieic5Tq8YMk/upload/e3a9ffe48af388d2f47544325a2a0b3c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem-overview">Problem Overview</h3>
<p>Given an integer <code>n</code>, return an array <code>ans</code> of length <code>n + 1</code> where <code>ans[i]</code> is the number of set bits in the binary representation of <code>i</code>.</p>
<ul>
<li><p><strong>Example:</strong> <code>n = 5</code> → Output: <code>[0, 1, 1, 2, 1, 2]</code></p>
</li>
<li><p><strong>Constraints:</strong> <code>0 &lt;= n &lt;= 10^5</code></p>
</li>
</ul>
<h3 id="heading-solution-1-brute-force">Solution 1: Brute Force</h3>
<p>This is the most intuitive approach. We iterate through each number from 0 to <code>n</code>, and for each number, we manually count its set bits.</p>
<p><strong>The Algorithm for counting bits of a single number</strong> <code>i</code><strong>:</strong></p>
<ol>
<li><p>Initialize a counter for the current number, e.g., <code>count = 0</code>.</p>
</li>
<li><p>Use a temporary variable <code>temp_num = i</code> to avoid modifying the main loop’s counter.</p>
</li>
<li><p>Use a <code>while</code> loop that continues as long as <code>temp_num</code> is not 0.</p>
</li>
<li><p>Check the LSB: Use the bitwise AND operator(&amp;) to check if the least significant bit is a 1.</p>
</li>
<li><p>Shift to the Next Bit: Use the right shift operator (<code>&gt;&gt;</code>) to discard the LSB.</p>
</li>
<li><p>The loop terminates when the number becomes 0.</p>
</li>
</ol>
<p>C++ implementation:</p>
<pre><code class="lang-cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> {</span>
<span class="hljs-keyword">public</span>:
    <span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; <span class="hljs-title">countBits</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
        <span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; <span class="hljs-title">ans</span><span class="hljs-params">(n + <span class="hljs-number">1</span>)</span></span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;= n; ++i) {
            <span class="hljs-comment">// Logic to count bits for each number 'i'</span>
            <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">int</span> temp_num = i;
            <span class="hljs-keyword">while</span> (temp_num != <span class="hljs-number">0</span>) {
                <span class="hljs-keyword">if</span> ((temp_num &amp; <span class="hljs-number">1</span>) == <span class="hljs-number">1</span>) { <span class="hljs-comment">// or simply: if (temp_num &amp; 1)</span>
                    count++;
                }
                temp_num = temp_num &gt;&gt; <span class="hljs-number">1</span>;
            }
            ans[i] = count;
        }
        <span class="hljs-keyword">return</span> ans;
    }
};
</code></pre>
<p>Complexity Analysis:</p>
<ul>
<li><p>Time Complexity: O(n * log(n))</p>
<ul>
<li><p>The outer <code>for</code> loop runs n + 1 times.</p>
</li>
<li><p>The inner <code>while</code> loop runs for a number of times equal to the number of bits in <code>i</code>, which is approximately log(i).</p>
</li>
</ul>
</li>
<li><p>Space Complexity: O(n)</p>
<ul>
<li>We need O(n) space for the <code>ans</code> array that we return.</li>
</ul>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[LeetCode 191. Number of 1 Bits]]></title><description><![CDATA[Problem Overview
The goal is to write a function that takes an unsigned integer and returns the number of ‘1’ bits it has in its binary representation. This value is also known as Hamming weight. This is the classic bit manipulation problem.
Solution...]]></description><link>https://blog.brucelinweb.com/leetcode-191-number-of-1-bits</link><guid isPermaLink="true">https://blog.brucelinweb.com/leetcode-191-number-of-1-bits</guid><category><![CDATA[leetcode]]></category><dc:creator><![CDATA[Bruce Lin]]></dc:creator><pubDate>Thu, 12 Jun 2025 17:07:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/ieic5Tq8YMk/upload/5329e971f072aed8c1b986f3118f730a.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem-overview">Problem Overview</h3>
<p>The goal is to write a function that takes an unsigned integer and returns the number of ‘1’ bits it has in its binary representation. This value is also known as Hamming weight. This is the classic bit manipulation problem.</p>
<h3 id="heading-solution-1-bit-by-bit-check">Solution 1: Bit-by-Bit Check</h3>
<p><strong>Core Idea:</strong></p>
<p>This approach checks each bit of the number individually.</p>
<ol>
<li><p><strong>Check LSB:</strong> Use the bitwise AND operator (<code>n &amp; 1</code>) to check if the Least Significant Bit (LSB) is a <code>1</code> . If it is, increment a counter. For example, 5 (<code>101</code>) &amp; 1 (<code>001</code>) results in 1.</p>
</li>
<li><p><strong>Right Shift:</strong> Shift the number one bit to the right (<code>n &gt;&gt; 1</code>) to discard the LSB and examine the next bit in the following iteration.</p>
</li>
<li><p><strong>Repeat:</strong> Continue these steps until the number becomes 0.</p>
</li>
</ol>
<p><strong>Time Complexity:</strong> O(1), as the loop runs a fixed number of times (32 for a 32-bit integer).</p>
<pre><code class="lang-cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> {</span>
<span class="hljs-keyword">public</span>:
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">hammingWeight</span><span class="hljs-params">(<span class="hljs-keyword">uint32_t</span> n)</span> </span>{
        <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">while</span> (n != <span class="hljs-number">0</span>) {
            <span class="hljs-comment">// check if LSB == 1 </span>
            <span class="hljs-keyword">if</span> (n &amp; <span class="hljs-number">1</span>) {
                count++;
            }
            <span class="hljs-comment">// right shift</span>
            n &gt;&gt;= <span class="hljs-number">1</span>;
        }
        <span class="hljs-keyword">return</span> count;
    }
};
</code></pre>
<h3 id="heading-solution-2-clear-rightmost-set-bit-optimized">Solution 2: Clear Rightmost Set Bit (Optimized)</h3>
<p><strong>Core Idea:</strong></p>
<p>This clever approach uses a trick to eliminate one set bit at a time.</p>
<ol>
<li><p><strong>Key Operation:</strong> This operation <code>n &amp; (n - 1)</code> clears the rightmost set bit (’1’) of n. For example, if n is 12 (<code>1100</code>), <code>n-1</code> is 11 (<code>1011</code>). <code>n &amp; (n - 1)</code> results in 8 (<code>1000</code>), effectively removing the rightmost ‘1’.</p>
</li>
<li><p><strong>Count and Repeat:</strong> We apply this operation repeatedly and increment a counter each time until n becomes 0. The total count is the number of set bits.</p>
</li>
</ol>
<p><strong>Time Complexity:</strong> O(m), where m is the number of set bits. This is highly efficient for numbers with few ‘1’s (sparse numbers).</p>
<pre><code class="lang-cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> {</span>
<span class="hljs-keyword">public</span>:
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">hammingWeight</span><span class="hljs-params">(<span class="hljs-keyword">uint32_t</span> n)</span> </span>{
        <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">while</span> (n != <span class="hljs-number">0</span>) {
            n = n &amp; (n - <span class="hljs-number">1</span>);
            count++;
        }
        <span class="hljs-keyword">return</span> count;
    }
};
</code></pre>
]]></content:encoded></item></channel></rss>