<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: we_are_broken_compilers</title>
    <description>The latest articles on Forem by we_are_broken_compilers (@we_are_broken_compilers).</description>
    <link>https://forem.com/we_are_broken_compilers</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3572212%2Fd1141b7c-3b08-4ed0-bd6f-73d8e9ac2cf9.jpeg</url>
      <title>Forem: we_are_broken_compilers</title>
      <link>https://forem.com/we_are_broken_compilers</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/we_are_broken_compilers"/>
    <language>en</language>
    <item>
      <title>Largest Subarray with 0 sum</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Tue, 21 Apr 2026 09:09:39 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/largest-subarray-with-0-sum-48ja</link>
      <guid>https://forem.com/we_are_broken_compilers/largest-subarray-with-0-sum-48ja</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an array of integers, find the maximum length of a subarray whose sum is 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt;&lt;br&gt;
arr = [15, -2, 2, -8, 1, 7, 10, 23]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
5&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation:
&lt;/h2&gt;

&lt;p&gt;The longest subarray with sum 0 is:&lt;br&gt;
[-2, 2, -8, 1, 7]&lt;/p&gt;

&lt;h2&gt;
  
  
  First Approach (Brute Force)
&lt;/h2&gt;

&lt;p&gt;The first idea that comes to mind is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fix a starting index i&lt;/li&gt;
&lt;li&gt;Keep adding elements till index j&lt;/li&gt;
&lt;li&gt;Check if the sum becomes 0&lt;/li&gt;
&lt;li&gt;Track the maximum length&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code (Java – Brute Force)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxLen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;maxLen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxLen&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxLen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem with this approach
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n²)&lt;/p&gt;

&lt;p&gt;For large inputs, this may cause TLE (Time Limit Exceeded)&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimized Approach (Prefix Sum + HashMap)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Main Idea :&lt;/strong&gt; If the same prefix sum appears again,&lt;br&gt;
then the subarray between those two indices has sum 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;prefixSum[j] - prefixSum[i] = 0&lt;br&gt;
&lt;strong&gt;prefixSum[j] == prefixSum[i]&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here prefixSum[j] is the sum of elements from &lt;strong&gt;0 to j&lt;/strong&gt; and prefixSum[i] is the sum of elements from &lt;strong&gt;0 to i&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;If both are same then ,The sum of elements between &lt;strong&gt;i+1 and j&lt;/strong&gt; becomes zero&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why HashMap?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because we need to:&lt;/p&gt;

&lt;p&gt;Store prefix sum&lt;/p&gt;

&lt;p&gt;Along with its first occurrence index&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimized Code (Java – O(n))
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// to handle subarray starting from index 0&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxLen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;containsKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;maxLen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxLen&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxLen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why do we store (0, -1) initially?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This handles the edge case when the subarray starts from index 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;arr = [1, -1]&lt;br&gt;
prefix sum at index 1 = 0&lt;br&gt;
length = 1 - (-1) = 2 &lt;/p&gt;

&lt;p&gt;So -1 represents a virtual index before the array starts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity :&lt;/strong&gt;O(n)&lt;br&gt;
We traverse the array only once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity :&lt;/strong&gt; O(n)&lt;br&gt;
Due to HashMap storing prefix sums.&lt;/p&gt;

&lt;p&gt;So this is all about this problem&lt;br&gt;
If you found this solution even a bit worst&lt;br&gt;
Please make sure to hit like button and also share it your coding fellows&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>java</category>
      <category>datastructures</category>
      <category>programming</category>
    </item>
    <item>
      <title>Koko Eating Banana</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Mon, 20 Apr 2026 14:37:27 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/koko-eating-banana-33ag</link>
      <guid>https://forem.com/we_are_broken_compilers/koko-eating-banana-33ag</guid>
      <description>&lt;p&gt;So by reading the heading, what comes to your mind?&lt;/p&gt;

&lt;p&gt;Someone named &lt;strong&gt;Koko&lt;/strong&gt; is eating bananas, right? &lt;br&gt;
Exactly — and the problem statement is pretty much about that.&lt;/p&gt;

&lt;p&gt;The problem says there is a monkey named &lt;strong&gt;Koko&lt;/strong&gt; who loves to eat bananas. We are given an array of banana piles, where each element represents the number of bananas in a pile.&lt;/p&gt;

&lt;p&gt;Our task is to find the &lt;strong&gt;minimum number of bananas Koko should eat per hour&lt;/strong&gt; so that she can finish all the bananas within &lt;strong&gt;&lt;code&gt;k&lt;/code&gt; hours&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  First Thought on Reading the Problem
&lt;/h2&gt;

&lt;p&gt;Once you read the problem, what does it remind you of?&lt;/p&gt;

&lt;p&gt;Something we’ve already done before, right?&lt;br&gt;
&lt;strong&gt;Minimum Time to Complete Trips&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So the first advice is:&lt;br&gt;
&lt;strong&gt;Try to attempt the problem yourself before jumping to the solution.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;p&gt;If you look closely, this is a &lt;strong&gt;monotonic problem&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If Koko can eat all bananas at speed &lt;code&gt;x&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;then she can also eat all bananas at any speed &lt;strong&gt;greater than &lt;code&gt;x&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This monotonic behavior immediately hints toward &lt;strong&gt;Binary Search on the Answer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The problem is very similar to &lt;em&gt;Minimum Time to Complete Trips&lt;/em&gt;, with just a few extra checks and a small formula adjustment.&lt;/p&gt;




&lt;h2&gt;
  
  
  Solution Approach
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Binary Search Boundaries&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Lower bound (&lt;code&gt;lb&lt;/code&gt;) = &lt;code&gt;1&lt;/code&gt;
  → Because Koko can eat at least one banana per hour&lt;/li&gt;
&lt;li&gt;Upper bound (&lt;code&gt;ub&lt;/code&gt;) = &lt;code&gt;maxPile&lt;/code&gt;
  → At most, she eats the largest pile in one hour&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Binary Search Process&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Compute &lt;code&gt;mid&lt;/code&gt; as a candidate eating speed&lt;/li&gt;
&lt;li&gt;Check if it is &lt;strong&gt;possible&lt;/strong&gt; to eat all bananas at this speed within 
&lt;code&gt;k&lt;/code&gt; hours&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Decision Making&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* If it **is possible**:

 -  Reduce the upper bound: `ub = mid`
 -  Because *“Itne mein toh ho hi jaega”*
   -&amp;gt; Now search for a better (smaller) speed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If it &lt;strong&gt;is not possible&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increase the lower bound: &lt;code&gt;lb = mid + 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;So that Koko eats a bit more bananas per hour&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Feasibility Check Logic
&lt;/h2&gt;

&lt;p&gt;For each pile:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculate how many full hours are needed using division&lt;/li&gt;
&lt;li&gt; If some bananas remain, add &lt;strong&gt;one extra hour&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; Keep accumulating total time&lt;/li&gt;
&lt;li&gt; If at any point &lt;code&gt;time &amp;gt; k&lt;/code&gt;, return &lt;code&gt;false&lt;/code&gt; immediately&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code (Java)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isPossible&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;piles&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bananaAte&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pile&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;piles&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;pile&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;bananaAte&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pile&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;bananaAte&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;kokoEat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MIN_VALUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isPossible&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time Complexity
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;code&gt;O(n log maxPile)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;br&gt;
Let:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; = number of piles&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;maxPile&lt;/code&gt; = maximum bananas in a pile&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Binary Search
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Search space: &lt;code&gt;1 → maxPile&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Time taken: &lt;code&gt;O(log maxPile)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Feasibility Check (&lt;code&gt;isPossible&lt;/code&gt;)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Traverses all piles&lt;/li&gt;
&lt;li&gt;Time taken: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Total
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n log maxPile)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Space Complexity
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;code&gt;O(1)&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;No extra data structures used&lt;/li&gt;
&lt;li&gt;Only a few variables&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Words
&lt;/h2&gt;

&lt;p&gt;So that’s all about the &lt;strong&gt;Koko Eating Bananas&lt;/strong&gt; problem &lt;/p&gt;

&lt;p&gt;If you’re still reading my blog till here —&lt;br&gt;
&lt;strong&gt;Thank you so much, buddy!&lt;/strong&gt; &lt;br&gt;
Please make sure to share it with your code fellows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy Coding!!&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;If you want, I can also:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make it &lt;strong&gt;more beginner-friendly&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;shorten it for &lt;strong&gt;LinkedIn/LeetCode discussion&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;or add &lt;strong&gt;visual examples&lt;/strong&gt; for better intuition&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>coding</category>
      <category>computerscience</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Minimum Time to Complete Trips</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Tue, 03 Feb 2026 14:00:46 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/minimum-time-to-complete-trips-49hn</link>
      <guid>https://forem.com/we_are_broken_compilers/minimum-time-to-complete-trips-49hn</guid>
      <description>&lt;h2&gt;
  
  
  Hey fellow developers 👋
&lt;/h2&gt;

&lt;p&gt;Welcome back to our DSA learning series!&lt;/p&gt;

&lt;p&gt;In this series, we focus on problems that may look simple at first, but often require deeper thinking to solve efficiently. We are learning DSA just like you, so our goal is always to build intuition first — before jumping into code.&lt;/p&gt;

&lt;p&gt;In this post, we are going to explore a problem that teaches you an important concept called &lt;strong&gt;Binary Search on Answer&lt;/strong&gt; — &lt;em&gt;Minimum Time to Complete Trips&lt;/em&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;We are given an array &lt;code&gt;time[]&lt;/code&gt;, where &lt;code&gt;time[i]&lt;/code&gt; represents how much time the &lt;em&gt;i-th bus&lt;/em&gt; takes to complete one trip.&lt;/p&gt;

&lt;p&gt;Our goal is to find the &lt;strong&gt;minimum time&lt;/strong&gt; required to complete &lt;code&gt;totalTrips&lt;/code&gt; trips when all buses are working in parallel.&lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding the Idea
&lt;/h2&gt;

&lt;p&gt;Each bus keeps running continuously.&lt;/p&gt;

&lt;p&gt;So, in a given time &lt;code&gt;T&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A bus with time &lt;code&gt;t&lt;/code&gt; can complete &lt;code&gt;T / t&lt;/code&gt; trips
&lt;/li&gt;
&lt;li&gt;All buses together can complete the sum of these trips
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our task is to find the &lt;strong&gt;smallest time &lt;code&gt;T&lt;/code&gt;&lt;/strong&gt; for which total trips ≥ &lt;code&gt;totalTrips&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Brute Force Approach
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Idea
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Start checking from time &lt;code&gt;T = 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For each &lt;code&gt;T&lt;/code&gt;, calculate how many trips all buses can complete
&lt;/li&gt;
&lt;li&gt;Stop when trips ≥ required trips
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Brute Force Code (Java)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;minimumTime&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;totalTrips&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// trips by this bus in T time&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;totalTrips&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Why &lt;code&gt;trips += T / t&lt;/code&gt; Works
&lt;/h3&gt;

&lt;p&gt;If:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;time[i] = 3
T = 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6 / 3 = 2 trips
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So dividing time gives us the number of trips completed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complexity (Brute Force)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;code&gt;O(n × ans)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; → number of buses
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ans&lt;/code&gt; → minimum required time
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since &lt;code&gt;ans&lt;/code&gt; can be very large, this approach will &lt;strong&gt;TLE for big inputs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(1)&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Optimal Approach (Binary Search on Answer)
&lt;/h2&gt;

&lt;p&gt;Instead of checking every time value, we can search the answer efficiently using &lt;strong&gt;binary search&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;p&gt;Time is &lt;strong&gt;monotonic&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we can finish in &lt;code&gt;T&lt;/code&gt; time
&lt;/li&gt;
&lt;li&gt;Then we can also finish in &lt;code&gt;T + 1&lt;/code&gt;, &lt;code&gt;T + 2&lt;/code&gt;, ...
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So binary search is possible on time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Search Space
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lower Bound (lb)&lt;/strong&gt; = &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Upper Bound (ub)&lt;/strong&gt; = &lt;code&gt;min(time) × totalTrips&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because in the worst case, the fastest bus does all trips alone.&lt;/p&gt;




&lt;h2&gt;
  
  
  Binary Search Logic
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Find &lt;code&gt;mid&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Check if we can finish &lt;code&gt;totalTrips&lt;/code&gt; in &lt;code&gt;mid&lt;/code&gt; time
&lt;/li&gt;
&lt;li&gt;If yes → try smaller time
&lt;/li&gt;
&lt;li&gt;If no → increase time
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When &lt;code&gt;lb == ub&lt;/code&gt;, we get our answer.&lt;/p&gt;




&lt;h2&gt;
  
  
  Optimal Code (Java)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isAtleast&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;givenTime&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;totalTrips&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;givenTime&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;totalTrips&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;minimumTime&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;totalTrips&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MAX_VALUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;lb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;ub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;totalTrips&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lb&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ub&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lb&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ub&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lb&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isAtleast&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;totalTrips&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;ub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;lb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lb&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Complexity (Optimal)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;code&gt;O(n log(totalTrips))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Binary search on answer space
&lt;/li&gt;
&lt;li&gt;Each step takes &lt;code&gt;O(n)&lt;/code&gt; to check feasibility
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n × log(searchSpace))
≈ O(n log(totalTrips))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(1)&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This problem is a classic example of &lt;strong&gt;Binary Search on Answer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once you learn to recognize patterns like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;false false false true true true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Binary search becomes a powerful tool in your DSA journey.&lt;/p&gt;

&lt;p&gt;If you read till here — thank you so much!&lt;br&gt;&lt;br&gt;
If you found this helpful, please like and share it with your coding friends to help us build a stronger community.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>algorithms</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Minimum Rounds to Complete All Tasks</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Sun, 25 Jan 2026 12:27:00 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/minimum-rounds-to-complete-all-tasks-bo2</link>
      <guid>https://forem.com/we_are_broken_compilers/minimum-rounds-to-complete-all-tasks-bo2</guid>
      <description>&lt;p&gt;Hey fellow developers 👋&lt;/p&gt;

&lt;p&gt;Welcome back to our DSA learning series!&lt;/p&gt;

&lt;p&gt;In this series, we focus on problems that look simple at first but often confuse beginners because of tricky constraints and edge cases. We are learning DSA just like you, so the goal is always to break problems down logically instead of jumping straight into the code.&lt;/p&gt;

&lt;p&gt;In this post, we are going to discuss a greedy-based problem that helps you understand how frequency counting and optimal grouping work together to minimize operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task Rules&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In one round, you can complete either 2 or 3 tasks of the same difficulty&lt;/li&gt;
&lt;li&gt;You must complete all tasks&lt;/li&gt;
&lt;li&gt;If it’s not possible (for example, only one task of a difficulty is left), return -1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Example&lt;/em&gt;&lt;br&gt;
Input&lt;br&gt;
tasks = [2,2,3,3,2,4,4,4,4,4]&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;We can complete the tasks in the following way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Round 1 → 3 tasks of difficulty 2&lt;/li&gt;
&lt;li&gt;Round 2 → 2 tasks of difficulty 3&lt;/li&gt;
&lt;li&gt;Round 3 → 3 tasks of difficulty 4&lt;/li&gt;
&lt;li&gt;Round 4 → 2 tasks of difficulty 4&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the minimum number of rounds required is 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach (Greedy + HashMap)
&lt;/h2&gt;

&lt;p&gt;Since we need to group tasks by difficulty, the first thing that comes to mind is a HashMap.&lt;/p&gt;

&lt;p&gt;Step 1: Count frequency of each difficulty&lt;/p&gt;

&lt;p&gt;Store:&lt;/p&gt;

&lt;p&gt;difficulty &lt;em&gt;(key)&lt;/em&gt;→ number of occurrences &lt;em&gt;(Count)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Calculate rounds for each difficulty&lt;/p&gt;

&lt;p&gt;For each difficulty count:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If count == 1 → ❌ not possible → return -1&lt;/li&gt;
&lt;li&gt;If count % 3 == 0 → use all groups of 3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Otherwise → use as many groups of 3 as possible and one group of 2&lt;/p&gt;

&lt;p&gt;Why this works?&lt;/p&gt;

&lt;p&gt;A group of 3 is more efficient than 2 (fewer rounds)&lt;/p&gt;

&lt;p&gt;For any remainder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4 = 2 + 2&lt;/li&gt;
&lt;li&gt;5 = 3 + 2&lt;/li&gt;
&lt;li&gt;8 = 3 + 3 + 2
So we just add one extra round when it's not divisible by 3.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code (Java)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int minimumRounds(int[] tasks) {
        HashMap&amp;lt;Integer, Integer&amp;gt; mp = new HashMap&amp;lt;&amp;gt;();

        // Count frequency of each difficulty
        for (int task : tasks) {
            mp.put(task, mp.getOrDefault(task, 0) + 1);
        }

        int rounds = 0;

        // Calculate rounds
        for (int key : mp.keySet()) {
            int count = mp.get(key);

            if (count == 1) {
                return -1;
            }

            if (count % 3 == 0) {
                rounds += count / 3;
            } else {
                rounds += (count / 3) + 1;
            }
        }

        return rounds;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One loop to build the HashMap → O(n)&lt;/li&gt;
&lt;li&gt;One loop over unique difficulties → at most O(n)&lt;/li&gt;
&lt;li&gt;HashMap operations (get, put) are O(1) on average&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall: &lt;strong&gt;O(n)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt; O(n)&lt;/p&gt;

&lt;p&gt;HashMap stores frequencies of tasks&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Time Complexity Is Linear?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each task is processed once&lt;/li&gt;
&lt;li&gt;HashMap operations take constant time&lt;/li&gt;
&lt;li&gt;No nested loops&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So total operations grow linearly with input size.&lt;/p&gt;

&lt;p&gt;If you found this helpful, give it a like 👍 and share it with your fellow coders!&lt;br&gt;
Happy coding !!!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>algorithms</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Delete a Node in a Binary Search Tree (BST)</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Wed, 31 Dec 2025 17:34:36 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/delete-a-node-in-a-binary-search-tree-bst-3d96</link>
      <guid>https://forem.com/we_are_broken_compilers/delete-a-node-in-a-binary-search-tree-bst-3d96</guid>
      <description>&lt;p&gt;Hey fellow developers 👋&lt;br&gt;&lt;br&gt;
Welcome back to our DSA learning series!&lt;/p&gt;

&lt;p&gt;This series is all about picking problems that every beginner finds confusing at first — and breaking them down slowly and logically. We are students ourselves, and we know how overwhelming DSA can feel if you jump straight into the code without understanding &lt;em&gt;why&lt;/em&gt; something works.&lt;/p&gt;

&lt;p&gt;In this post, we are going to talk about a problem that really helps you understand how Binary Search Trees work beneath the surface:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deleting a node from a Binary Search Tree (BST).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At first, it may feel a bit complicated — especially when the node has children.&lt;br&gt;
But once you slow down and reason through each case, the logic becomes surprisingly neat and structured.&lt;/p&gt;

&lt;p&gt;Let us walk through it step by step.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Reminder: What Makes a BST Special?
&lt;/h2&gt;

&lt;p&gt;In a Binary Search Tree:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All values in the &lt;strong&gt;left subtree&lt;/strong&gt; are smaller than the node&lt;/li&gt;
&lt;li&gt;All values in the &lt;strong&gt;right subtree&lt;/strong&gt; are greater&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This ordering is exactly what allows us to efficiently search, insert, and delete.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given the root of a BST and a value &lt;code&gt;key&lt;/code&gt;. &lt;br&gt;
Your job is to delete the node with that key (if it exists) and return the updated tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The twist:&lt;/strong&gt; Deletion must be handled in a way that keeps the tree a valid BST.&lt;/p&gt;




&lt;h2&gt;
  
  
  Finding the Node First
&lt;/h2&gt;

&lt;p&gt;Before deleting anything, we first locate the node:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;key &amp;lt; root.val&lt;/code&gt; → go left&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;key &amp;gt; root.val&lt;/code&gt; → go right&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;equal&lt;/code&gt; → this is the node we want to delete&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Three Deletion Cases
&lt;/h2&gt;

&lt;p&gt;Once we find the node, there are only three possible scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 1: Node Has No Children (Leaf Node)
&lt;/h3&gt;

&lt;p&gt;This is the easiest case. If the node has no left and right child, we simply remove it.&lt;br&gt;
&lt;code&gt;return null;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 2: Node Has One Child
&lt;/h3&gt;

&lt;p&gt;If the node has only one child, we replace it with that child.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only right child → &lt;code&gt;return root.right&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Only left child → &lt;code&gt;return root.left&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Case 3: Node Has Two Children (The Interesting One)
&lt;/h3&gt;

&lt;p&gt;This is where most students get stuck — but it follows a very clean idea. We cannot just delete the node, because both sides still hold valid subtrees.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So we do this:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Find the inorder successor&lt;/strong&gt; (the smallest value in the right subtree).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Copy its value&lt;/strong&gt; into the current node.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Delete that successor node&lt;/strong&gt; from the right subtree.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Putting It All Together — Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {

    // finds the inorder successor (smallest node in right subtree)
    public static TreeNode inOrderSuccessor(TreeNode root) {
        while (root.left != null) {
            root = root.left;
        }
        return root;
    }

    public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null)
            return root;

        // searching for the node
        if (root.val &amp;gt; key) {
            root.left = deleteNode(root.left, key);
        } 
        else if (root.val &amp;lt; key) {
            root.right = deleteNode(root.right, key);
        } 
        else {
            // found
            // Case 1: no children
            if (root.left == null &amp;amp;&amp;amp; root.right == null) {
                return null;
            }

            // Case 2: one child
            if (root.left == null) {
                return root.right;
            } 
            else if (root.right == null) {
                return root.left;
            }

            // Case 3: two children
            TreeNode successor = inOrderSuccessor(root.right);
            root.val = successor.val;
            root.right = deleteNode(root.right, successor.val);
        }

        return root;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;What makes this problem powerful is not the code — it is the &lt;strong&gt;structured way of thinking&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Understand the BST property&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Identify the node&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Handle each deletion case calmly&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Preserve the structure&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once this mental model clicks, most tree problems suddenly feel more manageable.&lt;/p&gt;

&lt;p&gt;If you try implementing this yourself a couple of times, you will notice the logic becoming second nature.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding — and see you in the next article!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>datastructures</category>
      <category>bst</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Next Permutation — Intuition, Explanation, and Clean Implementation</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Thu, 11 Dec 2025 11:53:26 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/next-permutation-intuition-explanation-and-clean-implementation-1ojh</link>
      <guid>https://forem.com/we_are_broken_compilers/next-permutation-intuition-explanation-and-clean-implementation-1ojh</guid>
      <description>&lt;p&gt;Hey fellow developers 👋&lt;br&gt;&lt;br&gt;
Welcome back to our DSA learning series!&lt;/p&gt;

&lt;p&gt;This series is all about picking problems that every beginner finds confusing at first — and breaking them down slowly, logically, and humanly. We are students ourselves, and we know how overwhelming DSA can feel if you jump straight into the code without understanding &lt;em&gt;why&lt;/em&gt; something works.&lt;/p&gt;

&lt;p&gt;Today, we are exploring a classic problem that helps build strong intuition around arrays and ordering — &lt;strong&gt;Next Permutation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At first glance, it feels abstract and mathematical.&lt;br&gt;&lt;br&gt;
But once you understand the pattern behind permutations, the entire logic becomes surprisingly simple and elegant.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;We are given an array of integers representing a permutation.&lt;br&gt;&lt;br&gt;
Our goal is to rearrange the numbers to form the &lt;strong&gt;next lexicographically greater permutation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If such an arrangement does not exist (meaning the array is already the highest possible permutation), we return the &lt;strong&gt;lowest possible arrangement&lt;/strong&gt; — which is just the array sorted in ascending order.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;[1,2,3] → [1,3,2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[2,3,1] → [3,1,2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[3,2,1] → [1,2,3]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rearrangement must be &lt;strong&gt;in-place&lt;/strong&gt; and use &lt;strong&gt;constant extra memory&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Understanding the Core Idea
&lt;/h2&gt;

&lt;p&gt;Before we try to code anything, let us think about &lt;em&gt;what it means&lt;/em&gt; for one permutation to come after another in lexicographical order.&lt;/p&gt;

&lt;p&gt;Take this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 2 5 4 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to find the "next" arrangement.&lt;br&gt;&lt;br&gt;
What does "next" mean?&lt;/p&gt;

&lt;p&gt;It means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make the smallest possible change that results in a bigger permutation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we ask ourselves:&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 1: Finding the Pivot (the dip point)
&lt;/h3&gt;

&lt;p&gt;We scan from right to left and find the &lt;strong&gt;first place where the order breaks the descending pattern&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why descending?&lt;/p&gt;

&lt;p&gt;Because if the entire array is in descending order (like &lt;code&gt;[5,4,3,2,1]&lt;/code&gt;), it is already the largest permutation.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 *2* 5 4 3
   ↑
  2 &amp;gt; 4 fails
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the pivot is &lt;code&gt;2&lt;/code&gt;’s index.&lt;/p&gt;

&lt;p&gt;This pivot is the point where we can make a “small increase”.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2: Find the next greater element on the right
&lt;/h3&gt;

&lt;p&gt;To make the permutation just slightly larger, we swap the pivot with the &lt;strong&gt;next greater element&lt;/strong&gt; from its right side.&lt;/p&gt;

&lt;p&gt;For our example, our next element greater than 2 from the right is 3, so we swap it with 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 3 5 4 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 3: Reverse the remaining right portion
&lt;/h3&gt;

&lt;p&gt;Once we swap, the right portion is still in descending order.&lt;br&gt;&lt;br&gt;
Reversing it gives the smallest lexicographical tail i.e. our result will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 3 2 4 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step finalizes the next permutation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Algorithm Works
&lt;/h2&gt;

&lt;p&gt;Because lexicographical ordering is all about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Making the &lt;strong&gt;smallest possible increase&lt;/strong&gt; at the earliest position
&lt;/li&gt;
&lt;li&gt;Keeping the rest of the sequence &lt;strong&gt;as small as possible&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This ensures we get the next permutation — not just any greater permutation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Implementation (Java)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;nextPermutation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Step 1: Find pivot index&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// If no pivot found, array is in descending order&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Step 2: Find next greater element on the right of pivot&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Step 3: Reverse the descending suffix&lt;/span&gt;
        &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
            &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, the problem looks intimidating at first because it deals with lexicographical order — something we usually do not think about explicitly.&lt;/p&gt;

&lt;p&gt;But once you understand the three simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Find the pivot&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Swap with the next greater element&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reverse the tail&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;…the problem becomes extremely intuitive.&lt;/p&gt;

&lt;p&gt;This problem tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pattern recognition
&lt;/li&gt;
&lt;li&gt;Understanding of permutations
&lt;/li&gt;
&lt;li&gt;Logical reasoning
&lt;/li&gt;
&lt;li&gt;Ability to write clean in-place algorithms
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Master this once and it will stick with you forever.&lt;/p&gt;

&lt;p&gt;Happy coding, friends!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>interview</category>
      <category>datastructures</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Trapping Rain Water</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Sat, 29 Nov 2025 16:37:08 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/trapping-rain-water-hin</link>
      <guid>https://forem.com/we_are_broken_compilers/trapping-rain-water-hin</guid>
      <description>&lt;p&gt;Hey fellow developers, welcome back to our DSA learning series. We hope&lt;br&gt;
you are doing great and enjoying this journey with us. This series is&lt;br&gt;
all about exploring problems the way a beginner genuinely sees them. We&lt;br&gt;
are students ourselves, so we know exactly how confusing a problem can&lt;br&gt;
look at first glance, and how a simple explanation at the right moment&lt;br&gt;
can completely change the way you understand it. Today, we are going to&lt;br&gt;
look at a very popular and very interesting problem --- Trapping Rain&lt;br&gt;
Water.&lt;/p&gt;

&lt;p&gt;This is a problem that almost every interviewer loves to ask. It is&lt;br&gt;
marked as "Hard" on most platforms, but in reality, once you truly&lt;br&gt;
understand what the question is trying to communicate, the whole logic&lt;br&gt;
feels surprisingly intuitive. So let us take our time and walk through&lt;br&gt;
the thought process slowly, step by step.&lt;/p&gt;
&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;We are given an array of non-negative integers that represent the&lt;br&gt;
heights of vertical bars placed next to each other. Each bar has a width&lt;br&gt;
of 1 unit. Now imagine rainwater falling on top of these bars. Depending&lt;br&gt;
on the relative heights of the bars on the left and right, some amount&lt;br&gt;
of water might get trapped between them. Our task is to determine how&lt;br&gt;
much water gets accumulated in total.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; &lt;code&gt;[0,1,0,2,1,0,1,3,2,1,2,1]&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;6&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Brute Force Intuition
&lt;/h2&gt;

&lt;p&gt;Let us start with the most straightforward idea --- the one that comes&lt;br&gt;
to mind when you simply think about the situation as a person, not as a&lt;br&gt;
coder.&lt;/p&gt;

&lt;p&gt;Imagine you are standing at a particular bar of height &lt;code&gt;h&lt;/code&gt;. To figure&lt;br&gt;
out how much water can stay on top of this bar, you do not look only at&lt;br&gt;
the bar itself; you quickly glance to your left and right. Water can&lt;br&gt;
only stay if there is a taller boundary on both sides. So you check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  What is the tallest bar on my left?&lt;/li&gt;
&lt;li&gt;  What is the tallest bar on my right?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, the maximum water level above the current bar is determined by the&lt;br&gt;
shorter of these two heights. Because water will spill over the smallest&lt;br&gt;
boundary.&lt;/p&gt;

&lt;p&gt;So the water trapped on index &lt;code&gt;i&lt;/code&gt; becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;min(max height on left, max height on right) − current height
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This idea is clean and logical. Now, to convert this into code, the&lt;br&gt;
simplest approach is to run two loops for each index --- one scanning&lt;br&gt;
leftward, and another scanning rightward. That gives us a clear&lt;br&gt;
brute-force solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int trap(int[] height) {
    int n = height.length;
    int totalWater = 0;

    for (int i = 0; i &amp;lt; n; i++) {
        int maxLeft = 0;
        int maxRight = 0;

        // Find max on the left
        for (int j = i; j &amp;gt;= 0; j--) {
            maxLeft = Math.max(maxLeft, height[j]);
        }

        // Find max on the right
        for (int j = i; j &amp;lt; n; j++) {
            maxRight = Math.max(maxRight, height[j]);
        }

        int waterLevel = Math.min(maxLeft, maxRight) - height[i];
        totalWater += waterLevel;
    }

    return totalWater;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution works perfectly, and it is a good starting point because it forces us to understand the meaning behind the formula. However, it takes O(n²) time since for every index we scan the array twice. The space usage is constant, but the speed becomes an issue as input size grows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimized Approach Using Prefix Arrays
&lt;/h2&gt;

&lt;p&gt;Now that we understand the brute-force idea, let us refine it. Notice&lt;br&gt;
that in the brute force solution, we repeatedly calculate the same&lt;br&gt;
values --- the left maximum and right maximum for many positions. Why&lt;br&gt;
not compute them once and reuse?&lt;/p&gt;

&lt;p&gt;This leads to the prefix-max and suffix-max approach.&lt;/p&gt;

&lt;p&gt;We create an array &lt;code&gt;left[]&lt;/code&gt; where &lt;code&gt;left[i]&lt;/code&gt; stores the maximum height&lt;br&gt;
from index 0 to i.&lt;/p&gt;

&lt;p&gt;Similarly, we create a &lt;code&gt;right[]&lt;/code&gt; array where &lt;code&gt;right[i]&lt;/code&gt; stores the&lt;br&gt;
maximum height from index i to the last index.&lt;/p&gt;

&lt;p&gt;Once these two arrays are ready, we already have all the information needed to compute the trapped water at every index in O(1) time. The final loop just applies the same formula as before.&lt;/p&gt;

&lt;p&gt;Here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int trap(int[] height) {
        int n = height.length;
        if(n == 0) return 0;

        int[] left = new int[n];
        int[] right = new int[n];

        // Build prefix max array
        left[0] = height[0];
        for(int i = 1; i &amp;lt; n; ++i) {
            left[i] = Math.max(left[i - 1], height[i]);
        }

        // Build suffix max array
        right[n - 1] = height[n - 1];
        for(int i = n - 2; i &amp;gt;= 0; --i) {
            right[i] = Math.max(right[i + 1], height[i]);
        }

        int trapped = 0;
        for(int i = 0; i &amp;lt; n; ++i) {
            trapped += Math.min(left[i], right[i]) - height[i];
        }

        return trapped;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The time complexity now becomes O(n) and the logic becomes cleaner. The only trade-off is that we use two extra arrays, giving us O(n) space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final and Most Optimized Approach: Two-Pointer Technique
&lt;/h2&gt;

&lt;p&gt;Finally, once we understand how prefix and suffix maxima help us, we can&lt;br&gt;
push the optimization even further. The question becomes: Do we really need two whole arrays? The surprising answer is — no. We can keep track of everything we need using just two pointers (one starting from the left end and one from the right) and two variables (leftMax and rightMax) that update as we move inward. Here is the key observation: Water trapping at any point is limited by the smaller boundary between left and right. So, if height[left] is smaller, we focus on the left side. If height[right] is smaller, we shift our attention to the right side. This allows us to calculate trapped water on the fly while moving the pointers.&lt;/p&gt;

&lt;p&gt;Here is the final optimized code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int trap(int[] height) {
        int left = 0, right = height.length - 1;
        int leftMax = 0, rightMax = 0;
        int trapped = 0;

        while (left &amp;lt; right) {

            if (height[left] &amp;lt; height[right]) {
                if (height[left] &amp;gt;= leftMax) {
                    leftMax = height[left];
                } else {
                    trapped += leftMax - height[left];
                }
                left++;
            } else {
                if (height[right] &amp;gt;= rightMax) {
                    rightMax = height[right];
                } else {
                    trapped += rightMax - height[right];
                }
                right--;
            }
        }
        return trapped;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This version gives us both O(n) time and O(1) space — the optimal combination.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By the time you reach this point, you have seen how the thought process evolves naturally: starting from a basic observation, moving to a brute-force method, then improving it with precomputed arrays, and finally refining it into the elegant two-pointer solution. And this journey is extremely important, because in interviews, most companies are not just looking for the final optimized answer — they want to hear how you build your logic step by step.&lt;/p&gt;

&lt;p&gt;This problem is a great example of how understanding the idea is more valuable than memorizing the code. Once the concept becomes clear, all three approaches become easy to derive on your own.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>interview</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding the Logic Behind Two Sum — and Extending It to Three Sum</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Sat, 08 Nov 2025 18:10:50 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/understanding-the-logic-behind-two-sum-and-extending-it-to-three-sum-838</link>
      <guid>https://forem.com/we_are_broken_compilers/understanding-the-logic-behind-two-sum-and-extending-it-to-three-sum-838</guid>
      <description>&lt;p&gt;Welcome back to our DSA Learning Series — where we pick one problem at a time, understand its logic deeply, and write clean, beginner-friendly code.&lt;/p&gt;

&lt;p&gt;The Three Sum problem is a favorite among interviewers because it tests layered logical thinking — combining array traversal, the two-pointer technique, and duplicate handling.&lt;/p&gt;

&lt;p&gt;But before tackling Three Sum, it’s crucial to fully understand the Two Sum logic, especially when the array is sorted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Revisiting Two Sum (in a Sorted Array)&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Problem Statement&lt;/em&gt;&lt;br&gt;
Given a sorted array and a target sum, find two numbers whose sum equals the target.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Input: nums = [-3, -1, 0, 2, 4, 5], target = 3&lt;br&gt;&lt;br&gt;
Output: [1, 2]  -- since nums[1] + nums[2] = 3&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Intuition&lt;/em&gt;&lt;br&gt;
If the array is sorted, we can use the two-pointer approach instead of nested loops for efficiency.&lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Place one pointer at the start (left) and another at the end (right).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calculate the sum of both numbers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If sum == target, we found the pair.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If sum &amp;lt; target, move the left pointer forward (to increase the sum).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If sum &amp;gt; target, move the right pointer backward (to decrease the sum).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This way, we traverse the array only once — giving an O(n) solution.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Code: Two Sum (Sorted Array)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int left = 0;
int right = nums.length - 1;

while (left &amp;lt; right) {
    int sum = nums[left] + nums[right];

    if (sum == target) {
        System.out.println("Pair found: " + nums[left] + ", " + nums[right]);
        left++;
        right--;
    } else if (sum &amp;lt; target) {
        left++;
    } else {
        right--;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Extending the Logic to Three Sum&lt;/strong&gt;&lt;br&gt;
Once the Two Sum logic is clear, we can easily extend it to handle three numbers. Let's take a look at the below problem to understand the logic deeper.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt;&lt;br&gt;
Find all unique triplets in the array whose sum equals 0.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Input: nums = [-1, 0, 1, 2, -1, -4]&lt;br&gt;&lt;br&gt;
Output: [[-1, -1, 2], [-1, 0, 1]]&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step-by-Step Intuition&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Sort the array.&lt;br&gt;
Sorting helps handle duplicates and makes the two-pointer logic work properly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fix one element (nums[i]).&lt;br&gt;
Then apply the Two Sum approach on the remaining part of the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set a new target:&lt;br&gt;
Since we want nums[i] + nums[left] + nums[right] = 0,&lt;br&gt;
the new target becomes -nums[i].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Move pointers (left, right) based on the current sum.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If sum == 0, store the triplet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Skip duplicates to avoid repeated triplets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adjust pointers (left++, right--) accordingly.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Code: Three Sum (Two-Pointer Approach)&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrays.sort(nums);
List&amp;lt;List&amp;lt;Integer&amp;gt;&amp;gt; result = new ArrayList&amp;lt;&amp;gt;();

for (int i = 0; i &amp;lt; nums.length - 2; i++) {
    // Skip duplicate elements for the first number
    if (i &amp;gt; 0 &amp;amp;&amp;amp; nums[i] == nums[i - 1]) continue;

    int left = i + 1;
    int right = nums.length - 1;

    while (left &amp;lt; right) {
        int sum = nums[i] + nums[left] + nums[right];

        if (sum == 0) {
            result.add(Arrays.asList(nums[i], nums[left], nums[right]));

            // Skip duplicates for left and right
            while (left &amp;lt; right &amp;amp;&amp;amp; nums[left] == nums[left + 1]) left++;
            while (left &amp;lt; right &amp;amp;&amp;amp; nums[right] == nums[right - 1]) right--;

            left++;
            right--;
        } else if (sum &amp;lt; 0) {
            left++;
        } else {
            right--;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The outer loop fixes one element (nums[i]).&lt;/p&gt;

&lt;p&gt;The inner loop uses the Two Sum logic to find pairs that sum to negative of nums[i].&lt;/p&gt;

&lt;p&gt;Sorting enables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Skipping duplicates easily.&lt;/li&gt;
&lt;li&gt;Using the two-pointer pattern effectively.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach efficiently finds all unique triplets in O(n²) time — which is optimal for this problem.&lt;/p&gt;

&lt;p&gt;What was your aha moment while reading this?&lt;br&gt;
Let us know — and don’t forget to share this post with someone preparing for their coding interviews!&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>programming</category>
      <category>sorting</category>
      <category>twosum</category>
    </item>
    <item>
      <title>Merge K Sorted Lists</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Thu, 30 Oct 2025 12:21:29 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/merge-k-sorted-lists-3ah7</link>
      <guid>https://forem.com/we_are_broken_compilers/merge-k-sorted-lists-3ah7</guid>
      <description>&lt;p&gt;Before we dive in, note that in our previous article we walked through how to merge two sorted linked lists, step by step. That idea is the core building block for what we will do here.&lt;/p&gt;

&lt;p&gt;If you would like a quick refresher first, check out the guide: 🔗 &lt;a href="https://dev.to/we_are_broken_compilers/merge-two-sorted-linked-lists-6il"&gt;Merge Two Sorted Linked Lists&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you’re comfortable with that, come back here — we’ll take it one level higher and learn how to merge K sorted lists efficiently.&lt;/p&gt;




&lt;p&gt;In this post we will break the problem down, explain the intuition, and walk through a clear example so the idea sticks&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given K linked lists — each sorted in ascending order.&lt;br&gt;
Your task is to merge them all into one single sorted linked list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Sample Input&lt;/em&gt;&lt;/strong&gt;: lists = [[1,4,5],[1,3,4],[2,6]]&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Sample output&lt;/em&gt;&lt;/strong&gt;: [1,1,2,3,4,4,5,6] &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Intuition — Think of It Like Paper Sheets&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Imagine you have 4 already sorted papers of numbers.&lt;/p&gt;

&lt;p&gt;If you merge all 4 one by one, like this:&lt;br&gt;
Merge 1 + 2 → result1&lt;br&gt;
Then result1 + 3 → result2&lt;br&gt;
Then result2 + 4 → result3&lt;/p&gt;

&lt;p&gt;That works… but it’s slow because you keep merging bigger and bigger lists repeatedly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A smarter way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Merge them in pairs!&lt;/p&gt;

&lt;p&gt;Round 1: Merge (List0 + List1), (List2 + List3)&lt;/p&gt;

&lt;p&gt;Round 2: Merge the two merged results&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Done !!!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is exactly what our code does — merge lists in pairs, doubling the group size each time.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;

        int interval = 1;
        int n = lists.length;

        while (interval &amp;lt; n) {
            for (int i = 0; i + interval &amp;lt; n; i = i + interval * 2) {
                lists[i] = mergeTwoLists(lists[i], lists[i + interval]);
            }
            interval *= 2;
        }

        return lists[0];
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Step-by-Step Explanation
&lt;/h2&gt;

&lt;p&gt;Step 1 — Base check&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (lists == null || lists.length == 0) return null;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If no lists exist, just return null.&lt;/p&gt;

&lt;p&gt;Step 2 — Start with interval = 1&lt;/p&gt;

&lt;p&gt;We’ll first merge lists that are next to each other:&lt;/p&gt;

&lt;p&gt;interval = 1 → merge (0,1), (2,3), (4,5)...&lt;/p&gt;

&lt;p&gt;Step 3 — Merge pairs inside a loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (int i = 0; i + interval &amp;lt; n; i = i + interval * 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That scary line simply means:&lt;/p&gt;

&lt;p&gt;Go through the lists array, merge list[i] with list[i + interval],&lt;br&gt;
then skip ahead by interval * 2 to jump to the next pair.&lt;/p&gt;

&lt;p&gt;So for:&lt;/p&gt;

&lt;p&gt;interval = 1: merge (0,1), (2,3)&lt;/p&gt;

&lt;p&gt;interval = 2: merge (0,2), (4,6)&lt;/p&gt;

&lt;p&gt;Step 4 — Replace the merged list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lists[i] = mergeTwoLists(lists[i], lists[i + interval]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After merging two sorted lists, store the result in lists[i].&lt;/p&gt;

&lt;p&gt;Step 5 — Double the interval&lt;br&gt;
interval *= 2;&lt;/p&gt;

&lt;p&gt;After each round, we’ve merged lists into bigger groups, so next time we double the interval.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Why interval doubles and i = i + interval * 2&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Why interval doubles and i = i + interval * 2&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Round&lt;/th&gt;
      &lt;th&gt;Interval&lt;/th&gt;
      &lt;th&gt;Pairs Merged&lt;/th&gt;
      &lt;th&gt;What Happens&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;(0,1), (2,3)&lt;/td&gt;
      &lt;td&gt;Merge small lists&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;(0,2)&lt;/td&gt;
      &lt;td&gt;Merge bigger merged lists&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;—&lt;/td&gt;
      &lt;td&gt;Done (only one list left)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each merge round combines 2×bigger lists,&lt;br&gt;
so we double the interval and jump ahead by interval * 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Time Complexity&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s break it down:&lt;/p&gt;

&lt;p&gt;Each merge operation takes O(N) (where N is total elements).&lt;/p&gt;

&lt;p&gt;Each round halves the number of lists.&lt;/p&gt;

&lt;p&gt;So total complexity = O(N log K).&lt;/p&gt;

&lt;p&gt;✅ Much faster than merging one by one (which is O(NK)).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Final Thoughts&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The key to understanding Merge K Sorted Lists is realizing that:&lt;/p&gt;

&lt;p&gt;You don’t merge them one-by-one — you merge them in pairs, in rounds.&lt;/p&gt;

&lt;p&gt;This divide-and-conquer pattern is clean, efficient, and elegant — it’s the same concept that powers merge sort!&lt;/p&gt;

</description>
      <category>coding</category>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Merge Two Sorted Linked Lists</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Sun, 26 Oct 2025 09:40:16 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/merge-two-sorted-linked-lists-6il</link>
      <guid>https://forem.com/we_are_broken_compilers/merge-two-sorted-linked-lists-6il</guid>
      <description>&lt;p&gt;Welcome back to our DSA Learning Series — where we pick one problem at a time, understand its logic deeply, and write clean, beginner-friendly Java code.&lt;/p&gt;

&lt;p&gt;The problem that we are going to discuss today is a classic in linked lists — &lt;strong&gt;Merge Two Sorted Lists&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is a foundational question that not only strengthens your understanding of linked list traversal but also acts as the building block for more advanced problems like &lt;strong&gt;Merge K Sorted Lists&lt;/strong&gt;, which we will explore in the next article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;We are given two sorted linked lists. Our task is to merge them into a single sorted linked list.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Input:&lt;br&gt;
List1: 1 → 3 → 5&lt;br&gt;
List2: 2 → 4 → 6&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
1 → 2 → 3 → 4 → 5 → 6&lt;/p&gt;
&lt;h2&gt;
  
  
  Intuition
&lt;/h2&gt;

&lt;p&gt;The idea is simple — both lists are already sorted. So we can use two pointers to traverse them by always picking the smaller value first and then continue until one list finishes, and then ultimately attach the remaining part of the other list.&lt;/p&gt;

&lt;p&gt;🧱 &lt;strong&gt;Step 1: Iterative Approach&lt;/strong&gt;&lt;br&gt;
In this approach, we use a dummy node to simplify pointer handling. This dummy node helps us build the final list step by step without worrying about the head pointer separately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummy = new ListNode(); // this is the dummy node to start the merged list
        ListNode pointer = dummy; // this pointer is used to build the new list

        while(list1 != null &amp;amp;&amp;amp; list2 != null) {
            if(list1.val &amp;lt; list2.val) {
                pointer.next = list1; // attach the smaller node
                list1 = list1.next;   // move list 1 pointer ahead
            } else {
                pointer.next = list2;
                list2 = list2.next; // move list 2 pointer ahead
            }

            pointer = pointer.next; // move result pointer ahead
        }

        // attach the remaining nodes
        if(list1 == null) {
            pointer.next = list2;
        } else {
            pointer.next = list1;
        }

        return dummy.next; // finally return the merged list formed at dummy's next
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-by-Step Example&lt;/strong&gt;&lt;br&gt;
Let’s merge the below two lists:&lt;br&gt;
List1 = 1 → 3 → 5&lt;br&gt;
List2 = 2 → 4 → 6&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt; 
&lt;thead&gt; &lt;tr&gt; &lt;th&gt;Step&lt;/th&gt; &lt;th&gt;list1&lt;/th&gt; &lt;th&gt;list2&lt;/th&gt; &lt;th&gt;Chosen Node&lt;/th&gt; &lt;th&gt;Merged List So Far&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;1 → 2&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;1 → 2 → 3&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;1 → 2 → 3 → 4&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;6&lt;/td&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;1 → 2 → 3 → 4 → 5&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;6&lt;/td&gt; &lt;td&gt;null&lt;/td&gt; &lt;td&gt;6&lt;/td&gt; &lt;td&gt;6&lt;/td&gt; &lt;td&gt;1 → 2 → 3 → 4 → 5 → 6&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; 
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  🌀 Step 2: Recursive Approach
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if(l1 == null) return l2;
    if(l2 == null) return l1;

    if(l1.val &amp;lt;= l2.val) {
        l1.next = mergeTwoLists(l2, l1.next);
        return l1;
    } else {
        l2.next = mergeTwoLists(l1, l2.next);
        return l2;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How the above approach works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The smaller node between &lt;code&gt;l1&lt;/code&gt; and &lt;code&gt;l2&lt;/code&gt; becomes part of the merged list.&lt;/li&gt;
&lt;li&gt;The function then recursively merges the rest of the nodes.&lt;/li&gt;
&lt;li&gt;The recursion continues until one list is empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This version is clean and expressive, though it uses extra call stack space (&lt;code&gt;O(n + m)&lt;/code&gt; in the worst case).&lt;/p&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;As each node is processed exactly once, the time complexity will be &lt;code&gt;O(n + m)&lt;/code&gt;, where n and m are the sizes of corresponding lists.&lt;/p&gt;

&lt;p&gt;Regarding the space complexity, it is constant for iterative solution - &lt;code&gt;O(1)&lt;/code&gt;. However recursive solution adds the stack space resulting in &lt;code&gt;O(n + m)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;This problem teaches one of the most important linked list patterns — &lt;strong&gt;merging while maintaining order&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It forms the foundation for more advanced problems like &lt;strong&gt;Merge K Sorted Lists&lt;/strong&gt;, which we will discuss in the next article by following a Divide and Conquer approach.&lt;/p&gt;

&lt;p&gt;💬 Your Turn:&lt;br&gt;
Try implementing both iterative and recursive versions yourself. and let us know which one feels more intuitive to you — pointer manipulation or recursion?&lt;/p&gt;

&lt;p&gt;Stay tuned and keep practicing!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>linkedlist</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Next Greater Element (Right) using Stack</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Thu, 23 Oct 2025 11:04:50 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/next-greater-element-right-using-stack-3ngn</link>
      <guid>https://forem.com/we_are_broken_compilers/next-greater-element-right-using-stack-3ngn</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Welcome back to Day 3 of our DSA Problem Series, where we explore one problem each day, understand its logic, and break it down into clean Java code.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Today’s challenge is a classic stack-based problem —&lt;br&gt;
👉 Find the Next Greater Element on the Right (NGE) for every element in the array.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Given an array of integers, for every element, find the next greater element that appears to its right.&lt;br&gt;
If there’s no greater element, store -1 for that position.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Input:&lt;/em&gt; &lt;strong&gt;[&lt;/strong&gt; 4, 5, 2, 10, 8 &lt;strong&gt;]&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Output:&lt;/em&gt; &lt;strong&gt;[&lt;/strong&gt; 5, 10, 10, -1, -1 &lt;strong&gt;]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For 4, next greater is 5&lt;/li&gt;
&lt;li&gt; For 5, next greater is 10 &lt;/li&gt;
&lt;li&gt; For 2, next greater is 10 &lt;/li&gt;
&lt;li&gt; For 10, no greater element → -1&lt;/li&gt;
&lt;li&gt; For 8, no greater element → -1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Brute Force Thought&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A straightforward approach would be:&lt;/p&gt;

&lt;p&gt;For each element, look to its right until you find a greater number.&lt;br&gt;
But that gives O(n²) time complexity — not ideal for large arrays 😓&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We can do better with a stack.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimized Stack-Based Solution (O(n))&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static int[] nextGreator(int[] arr) {
    Stack&amp;lt;Integer&amp;gt; st = new Stack&amp;lt;&amp;gt;();
    int[] result = new int[arr.length];

    for (int i = arr.length - 1; i &amp;gt;= 0; i--) {
        // Pop smaller or equal elements from stack
        while (!st.isEmpty() &amp;amp;&amp;amp; arr[i] &amp;gt;= arr[st.peek()]) {
            st.pop();
        }

        // If stack is empty, no greater element to the right
        result[i] = st.isEmpty() ? -1 : arr[st.peek()];

        // Push current index onto the stack
        st.push(i);
    }
    return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How this approach works...&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Idea:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Use a stack to keep track of elements for which we have not yet found the next greater element. Traverse the array from &lt;strong&gt;right to left&lt;/strong&gt; so that the elements to the right are already processed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Step-by-Step:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize an empty stack and a result array.
&lt;/li&gt;
&lt;li&gt;For each element (starting from the end of the array):

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pop smaller elements&lt;/strong&gt; from the stack, because they cannot be the next greater for the current element.
&lt;/li&gt;
&lt;li&gt;If the stack is empty after popping, there is &lt;strong&gt;no greater element to the right&lt;/strong&gt; — store &lt;code&gt;-1&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;If the stack is not empty, the element at the &lt;strong&gt;top of the stack&lt;/strong&gt; is the next greater element — store it in the result.
&lt;/li&gt;
&lt;li&gt;Push the &lt;strong&gt;current element (or its index)&lt;/strong&gt; onto the stack for future comparisons.
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Why it works:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The stack always contains elements that are &lt;strong&gt;potential candidates&lt;/strong&gt; for next greater elements of elements to their left.
&lt;/li&gt;
&lt;li&gt;Each element is pushed and popped &lt;strong&gt;at most once&lt;/strong&gt;, giving an efficient O(n) solution.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step-by-Step Dry Run&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s dry-run it for arr = [4, 5, 2, 10, 8].&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;i&lt;/th&gt;
      &lt;th&gt;arr[i]&lt;/th&gt;
      &lt;th&gt;Stack (indices)&lt;/th&gt;
      &lt;th&gt;Next Greater&lt;/th&gt;
      &lt;th&gt;Result[]&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;[]&lt;/td&gt;
      &lt;td&gt;-1&lt;/td&gt;
      &lt;td&gt;[-,-,-,-,-1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;[4]&lt;/td&gt;
      &lt;td&gt;-1&lt;/td&gt;
      &lt;td&gt;[-,-,-,-1,-1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;[3]&lt;/td&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;[-,-,10,-1,-1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;[3]&lt;/td&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;[-,10,10,-1,-1]&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;[1,3]&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;[5,10,10,-1,-1]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Output: [5, 10, 10, -1, -1]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time and Space Complexity comparison&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Operation&lt;/th&gt;
      &lt;th&gt;Description&lt;/th&gt;
      &lt;th&gt;Complexity&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Traversal of array&lt;/td&gt;
      &lt;td&gt;Each element is visited once (from right to left)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stack operations (push/pop)&lt;/td&gt;
      &lt;td&gt;Each element is pushed and popped at most once&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Total Time Complexity&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Traversal + Stack operations&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Stack + Result array&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This problem forms the base for many variations like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next Greater Element (Left)&lt;/li&gt;
&lt;li&gt;Next Smaller Element (Right/Left)&lt;/li&gt;
&lt;li&gt;Stock Span Problem&lt;/li&gt;
&lt;li&gt;Largest Rectangle in Histogram&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So make sure you truly understand this one — it’ll come in handy often!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your Turn!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Try modifying the code for:&lt;/p&gt;

&lt;p&gt;🔁 Next Smaller Element (Right)&lt;/p&gt;

&lt;p&gt;Can you spot the small change needed? 😉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Bonus Challenge:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Also, try writing the brute force solution and analyze its time complexity.&lt;br&gt;
Can you explain why the stack-based approach is faster?&lt;/p&gt;

&lt;p&gt;💬 Share your version or your reasoning in the comments below — We'd love to see how you approach it!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Delete N Nodes After M Nodes of a Linked List</title>
      <dc:creator>we_are_broken_compilers</dc:creator>
      <pubDate>Wed, 22 Oct 2025 17:32:17 +0000</pubDate>
      <link>https://forem.com/we_are_broken_compilers/delete-n-nodes-after-m-nodes-of-a-linked-list-2h71</link>
      <guid>https://forem.com/we_are_broken_compilers/delete-n-nodes-after-m-nodes-of-a-linked-list-2h71</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey everyone!&lt;br&gt;
This is our second article in the DSA learning series where we aim to grow together — one problem at a time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our goal remains the same: to make concepts simpler, logic clearer, and learning collaborative.&lt;/strong&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;In this problem we are given a linked list, and we have to perform the following operations:&lt;br&gt;
&lt;strong&gt;Keep M nodes, then delete N nodes&lt;/strong&gt;, and keep repeating this until the end of the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
Input:&lt;br&gt;
Linked List: 9 → 1 → 3 → 5 → 9 → 4 → 10 → 1&lt;br&gt;&lt;br&gt;
M = 2, N = 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
9 → 1 → 5 → 9 → 10 → 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
Skip 2 nodes (9, 1), delete the next 1 (3), then skip 2 again (5, 9), delete next (4), and continue.&lt;/p&gt;



&lt;h2&gt;
  
  
  Thought Process
&lt;/h2&gt;

&lt;p&gt;This is one of those pointer problems where clarity beats complexity.&lt;/p&gt;

&lt;p&gt;We can solve this cleanly using just one loop — by breaking the task into repeatable chunks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Skip M nodes&lt;/strong&gt; — these are the nodes we keep.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Delete N nodes&lt;/strong&gt; — we move a second pointer ahead by N, cutting them out of the chain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Connect&lt;/strong&gt; the last kept node to the node after the deleted segment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Repeat&lt;/strong&gt; until the list ends.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach is &lt;strong&gt;iterative&lt;/strong&gt;, &lt;strong&gt;clean&lt;/strong&gt;, and &lt;strong&gt;runs in O(length of list)&lt;/strong&gt;.&lt;/p&gt;



&lt;h2&gt;
  
  
  Code Implementation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static void linkdelete(Node   head, int m, int n) {
    Node temp = head;

    while (temp != null) {
        // Skip M nodes (we are already on the 1st one)
        int countM = 1;
        while (countM &amp;lt; m &amp;amp;&amp;amp; temp != null) {
            temp = temp.next;
            countM++;
        }

        // If we reached the end, nothing to delete
        if (temp == null) return;

        // Start deleting next N nodes
        Node sampleTemp = temp.next;
        int countN = 1;
        while (countN &amp;lt;= n &amp;amp;&amp;amp; sampleTemp != null) {
            sampleTemp = sampleTemp.next;
            countN++;
        }

        // Connect Mth node to the node after deleted segment
        temp.next = sampleTemp;

        // Move temp to the next segment
        temp = sampleTemp;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;⚙️ Time and Space Complexity&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Type&lt;/th&gt;
      &lt;th&gt;Complexity&lt;/th&gt;
      &lt;th&gt;Explanation&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Time&lt;/td&gt;
      &lt;td&gt;O(L)&lt;/td&gt;
      &lt;td&gt;We traverse each node once&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Space&lt;/td&gt;
      &lt;td&gt;O(1)&lt;/td&gt;
      &lt;td&gt;Only pointers and counters used&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;This problem is a great exercise in pointer control and in-place modification.&lt;br&gt;
We hope our explanation helped you connect the logic step by step and see how iterative thinking simplifies complex pointer tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;💬 We’d love your support!&lt;br&gt;
Share your feedback, ask questions, and let’s keep learning together — one DSA problem at a time.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;DSA Series by Broken Compilers.&lt;br&gt;
Follow our journey as we learn, teach, and grow through code.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>programming</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
