<?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[DSA & Discuss]]></title><description><![CDATA[DSA & Discuss]]></description><link>https://dsadiscuss.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 24 Oct 2026 06:05:47 GMT</lastBuildDate><atom:link href="https://dsadiscuss.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Stop Thinking Binary Search Is Only for Searching 🔍]]></title><description><![CDATA[Learn to spot binary search problems with 3 simple questions. Step-by-step examples: sorted array and Koko Eating Bananas, in plain English.]]></description><link>https://dsadiscuss.hashnode.dev/binary-search</link><guid isPermaLink="true">https://dsadiscuss.hashnode.dev/binary-search</guid><category><![CDATA[Algorithms Data Structures ]]></category><category><![CDATA[DSA]]></category><category><![CDATA[problem solving skills]]></category><category><![CDATA[leetcode-solution]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Binary Search Algorithm]]></category><category><![CDATA[Thinking]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[swayanprabha]]></dc:creator><pubDate>Tue, 20 Oct 2026 10:20:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6ad713ae2ebb3a56f1b93e26/16b7c195-bf8c-4869-bfab-6aaadc2fe23f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>It’s not an algorithm to memorize. Most of us learn binary search like this: “Sorted array. Find the target. Done.”</p>
<p>But the real skill is spotting when it fits, even when the problem has no sorted array at all.</p>
<p>Ask the problem 3 questions:</p>
<p>❓ Is there an order in which my answer lies? Is my data range sorted, or can I treat it as ordered (small → big)?</p>
<p>❓ Does my answer live in that fixed range? Is it always between a min and a max?</p>
<p>❓ What kind of problem is it?</p>
<p>Type 1: Find the answer inside the data range given. Example: find the position of a number in a sorted array.</p>
<p>Type 2: Guess an answer and test it against a range value. Example: Koko must eat all banana piles within a given number of hours. What is the slowest eating speed that works?</p>
<p>Now make an assumption 🎯</p>
<p>Say my answer is exactly in the middle. After checking, I have only 3 choices:</p>
<ol>
<li><p>The middle is the answer ✅ I found it. Stop.</p>
</li>
<li><p>The middle is smaller than what I need ⬅️ Everything at or below the middle cannot be my answer. 👉 Ignore that part. Look at the right half.</p>
</li>
<li><p>The middle is bigger than what I need ➡️ Everything at or above the middle cannot be my answer. 👉 Ignore that part. Look at the left half.</p>
</li>
</ol>
<p>Repeat: assume → check → ignore one side.</p>
<p>Stop when no data is left to check. If you never found a match, the answer is not there.</p>
<h2>So idea 💡</h2>
<p>Binary search is just this: Make one smart guess in the middle. Check it. Safely throw away half. Every round, the problem gets half as big.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6ad713ae2ebb3a56f1b93e26/73bb0cd9-5678-41d2-8c18-b6e7672b2035.png" alt="" style="display:block;margin:0 auto" />

<p><strong>Example: Capacity To Ship Packages Within D Days</strong><br />Link: <a href="https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/">https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/</a></p>
<p>A conveyor belt has packages with weights. A ship must carry all of them within D days. Packages must go in the given order. You cannot reorder them. Each day, you load packages onto the ship in order, but the total weight cannot go over the ship’s capacity. Find the smallest capacity that finishes all packages within D days.</p>
<p>Input: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] D = 5</p>
<p>Output : 15</p>
<p>Now let's solve this problem using the above discussed process.</p>
<h2><strong>Solution:</strong></h2>
<h3><strong>Step 0: Ask the problem the questions</strong></h3>
<ul>
<li><p><strong>❓ Is there an order? The answer is a capacity (a number). Bigger capacity=smaller days and small capacity=more days. So small → big capacities behave in order. Yes.</strong></p>
</li>
<li><p><strong>❓ Is the answer in a fixed range? Yes. the lowest capacity it will have is (highest of the weights) and highest capacity will be (sum of all the weights)</strong></p>
</li>
<li><p><strong>❓ Find it in the data, or guess and test? The answer is not inside the weights list. We guess a capacity and test it. Type 2.</strong></p>
</li>
</ul>
<p><strong>Finding the range:</strong></p>
<ul>
<li><p><strong>Smallest possible = 10. The ship must at least carry the heaviest package. Anything less cannot carry it at all.</strong></p>
</li>
<li><p><strong>Biggest possible = 55. That is the sum of all weights. The ship carries everything in 1 day. A bigger ship is useless.</strong></p>
</li>
</ul>
<pre><code class="language-plaintext">Capacity:  10 ............................................ 55
           ^ heaviest package                    all packages ^
           (min)                                  in one day (max)
</code></pre>
<h3><strong>The yes/no test (we repeat this every round)</strong></h3>
<blockquote>
<p><strong>“If the ship’s capacity is X, can I finish in 5 days or less?”</strong></p>
</blockquote>
<p><strong>test: load packages in order. When the next package does not fit, start a new day. Count the days.</strong></p>
<ul>
<li><p><strong>Days ≤ 5 → YES. This capacity works, it may be our answer(save it). but we want a smaller one if that works too, so ignore the bigger side.</strong></p>
</li>
<li><p><strong>Days &gt; 5 → NO. The ship is too small. Ignore the smaller side.</strong></p>
</li>
</ul>
<h3><strong>Solving step by step</strong></h3>
<p><strong>Round 1: range 10 to 55 | Middle = (10 + 55) ÷ 2 ≈ 32</strong></p>
<p><strong>Test capacity 32:</strong></p>
<pre><code class="language-plaintext">Day 1: 1+2+3+4+5+6+7 = 28   (adding 8 → 36, too much)
Day 2: 8+9+10        = 27  2 days. 2 ≤ 5, so YES ✅
Decision: 32 works, but a smaller one may work too. Ignore everything above 32.
</code></pre>
<p><strong>Round 2: range 10 to 32 | Middle = 21</strong></p>
<ul>
<li><strong>Test capacity 21:</strong></li>
</ul>
<pre><code class="language-plaintext">Day 1: 1+2+3+4+5+6 = 21
Day 2: 7+8         = 15   (adding 9 → 24, too much)
Day 3: 9+10        = 19      3 days. YES ✅
Decision: Ignore everything above 21.(we want smallest)
</code></pre>
<p><strong>Round 3: range 10 to 21 | Middle = 15</strong></p>
<ul>
<li><strong>Test capacity 15:</strong></li>
</ul>
<pre><code class="language-plaintext">Day 1: 1+2+3+4+5 = 15
Day 2: 6+7       = 13
Day 3: 8         = 8     (adding 9 → 17, too much)
Day 4: 9         = 9     (adding 10 → 19, too much)
Day 5: 10        = 10     5 days. 5 ≤ 5, so YES ✅
Decision: Ignore everything above 15.
</code></pre>
<p><strong>Round 4: range 10 to 15 | Middle = 12</strong></p>
<ul>
<li><strong>Test capacity 12:</strong></li>
</ul>
<pre><code class="language-plaintext">Day 1: 1+2+3+4 = 10
Day 2: 5+6     = 11
Day 3: 7       = 7
Day 4: 8       = 8
Day 5: 9       = 9
Day 6: 10      = 10   6 days. 6 &gt; 5, so NO ❌
Decision: 12 is too small, so 10, 11 and 12 are all too small. Ignore everything up to 12.
</code></pre>
<p><strong>Round 5: range 13 to 15 | Middle = 14</strong></p>
<p><strong>the total days will be 6 , which is &gt;5 so will shift to higher side, and only one left : 15</strong></p>
<p><strong>we have already tested it. so the range finishes here and the answer is 15.</strong></p>
<pre><code class="language-plaintext">Capacity: 10  11  12  13  14 | 15  16 ... 32 ... 55
           ❌  ❌  ❌  ❌  ❌ |  ✅  ✅     ✅      ✅
                              ^
                  smallest capacity that works
</code></pre>
<p><strong>Answer: 15 🎉</strong></p>
<h3>What to notice</h3>
<ul>
<li><p><strong>Only 5 tests</strong> instead of trying all 46 capacities from 10 to 55.</p>
</li>
<li><p>Every test is the same yes/no question, and every answer let us throw away a big part of the range.</p>
</li>
<li><p>This works because of one rule: <strong>bigger capacity never needs more days.</strong> If this rule did not hold, we could not throw anything away.</p>
</li>
</ul>
<h3>Some popular examples on this algorithm:</h3>
<p>Search these names on Google or LeetCode.</p>
<p><strong>Type 1: find it inside the data</strong></p>
<ul>
<li><p>Binary Search (the basic one)</p>
</li>
<li><p>Search Insert Position</p>
</li>
<li><p>First and Last Position of an Element in a Sorted Array</p>
</li>
<li><p>Search in Rotated Sorted Array</p>
</li>
<li><p>Find Minimum in Rotated Sorted Array</p>
</li>
<li><p>Find Peak Element</p>
</li>
<li><p>Search a 2D Matrix</p>
</li>
</ul>
<p><strong>Type 2: guess an answer and test it</strong></p>
<ul>
<li><p>Koko Eating Bananas</p>
</li>
<li><p>Capacity to Ship Packages Within D Days</p>
</li>
<li><p>Sqrt(x), the square root of a number</p>
</li>
<li><p>Split Array Largest Sum</p>
</li>
<li><p>Minimum Days to Make m Bouquets</p>
</li>
<li><p>Magnetic Force Between Two Balls</p>
</li>
<li><p>Aggressive Cows (a classic)</p>
</li>
<li><p>Allocate Books / Painter’s Partition (a classic)</p>
</li>
</ul>
<h3>Conclusion 🎯</h3>
<p>Binary search is not just for sorted arrays. It is a way of thinking.</p>
<ul>
<li><p>Ask three questions: Is there an order? Is the range fixed? Do I find the answer in the data, or guess and test?</p>
</li>
<li><p>Then repeat: <strong>assume</strong> the middle, <strong>test</strong> it, <strong>compare</strong> with the target, and <strong>ignore</strong> the half that cannot hold the answer.</p>
</li>
<li><p>Stop when you find it or nothing is left.</p>
</li>
</ul>
<p>Every round cuts the problem in half. So the real skill is not code. It is spotting <strong>when</strong> the pattern fits.</p>
]]></content:encoded></item></channel></rss>