<?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: Prashant Pandey</title>
    <description>The latest articles on Forem by Prashant Pandey (@prashant_pan_).</description>
    <link>https://forem.com/prashant_pan_</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%2F1499984%2F90f1c4a3-439c-4587-afbc-744246f9891a.png</url>
      <title>Forem: Prashant Pandey</title>
      <link>https://forem.com/prashant_pan_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prashant_pan_"/>
    <language>en</language>
    <item>
      <title>🚀 Today I Solved: Find the Largest/Smallest Subarray with Sum = K</title>
      <dc:creator>Prashant Pandey</dc:creator>
      <pubDate>Wed, 12 Nov 2025 04:19:28 +0000</pubDate>
      <link>https://forem.com/prashant_pan_/today-i-solved-find-the-largestsmallest-subarray-with-sum-k-2kc1</link>
      <guid>https://forem.com/prashant_pan_/today-i-solved-find-the-largestsmallest-subarray-with-sum-k-2kc1</guid>
      <description>&lt;p&gt;Some DSA problems just _click _— they remind you why problem-solving feels so satisfying.&lt;br&gt;
Today’s one was exactly that:&lt;br&gt;
&lt;strong&gt;“Find the largest or smallest subarray whose sum equals K.”&lt;br&gt;
**&lt;br&gt;
At first glance, it looks simple. But once you dig in, you start seeing the beauty of how **logic and data structures&lt;/strong&gt; work together.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;💭 My First Thought: Brute Force It&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
I started with the most obvious approach — two nested loops.&lt;/p&gt;

&lt;p&gt;For every starting point &lt;em&gt;i&lt;/em&gt;, I extended the subarray until &lt;em&gt;j _and checked if the sum matched _K&lt;/em&gt;.&lt;br&gt;
Whenever it did, I updated the max/min length.&lt;/p&gt;

&lt;p&gt;It worked… but it wasn’t efficient.&lt;br&gt;
As soon as the array got large, performance dropped fast.&lt;/p&gt;

&lt;p&gt;Time complexity? &lt;strong&gt;O(N²)&lt;/strong&gt; — not great.&lt;br&gt;
But honestly, brute force is always my &lt;em&gt;warm-up round&lt;/em&gt;. It helps me understand the pattern before optimizing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚙️ The Aha Moment: HashMap + Prefix Sum&lt;br&gt;
**&lt;br&gt;
Then came the real game changer — **prefix sums&lt;/strong&gt; and &lt;strong&gt;hashmaps&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of re-summing elements again and again, I stored cumulative sums.&lt;br&gt;
At each index &lt;em&gt;j&lt;/em&gt;, I looked for a previous prefix sum &lt;em&gt;p[i-1]&lt;/em&gt; such that:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;p[i-1] = p[j] - K&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If it existed, that meant the subarray between &lt;em&gt;(i, j)&lt;/em&gt; had a sum of &lt;em&gt;K&lt;/em&gt;.&lt;br&gt;
With that, I could instantly calculate the subarray length and update my max/min lengths — all in &lt;strong&gt;O(N)&lt;/strong&gt; time.&lt;/p&gt;

&lt;p&gt;Honestly, it felt _magical _ to watch the brute force approach melt into something elegant.&lt;/p&gt;

&lt;p&gt;****🧠 Key Takeaways&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Brute force teaches intuition&lt;/strong&gt;. Always start there — it helps you understand the logic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prefix sums + hashmaps&lt;/strong&gt; are super powerful for subarray and range problems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The best feeling? Watching your O(N²) solution drop to O(N). 😄&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;💬 Final Reflection&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
This problem was more than just about arrays and math — it was a lesson in &lt;em&gt;pattern recognition and problem simplification&lt;/em&gt;.&lt;br&gt;
Every DSA challenge like this builds that “aha!” muscle a little stronger.&lt;/p&gt;

&lt;p&gt;I coded it in **&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dydwxsjvjg1bxsmu45m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dydwxsjvjg1bxsmu45m.png" alt=" " width="800" height="777"&gt;&lt;/a&gt;**, but honestly, the concept stays the same in any language.&lt;br&gt;
If you haven’t tried this one yet — give it a go!&lt;/p&gt;

&lt;h1&gt;
  
  
  DSA #Java #CodingJourney #ProblemSolving #HashMap #PrefixSum #DataStructures #Algorithms #LearningEveryday
&lt;/h1&gt;

</description>
      <category>dsa</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
