<?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>DEV Community: M Umair Ullah</title>
    <description>The latest articles on DEV Community by M Umair Ullah (@umair01).</description>
    <link>https://dev.to/umair01</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%2F1921875%2F8a897764-4145-48b2-8510-67c482abe81f.jpg</url>
      <title>DEV Community: M Umair Ullah</title>
      <link>https://dev.to/umair01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/umair01"/>
    <language>en</language>
    <item>
      <title>🔥 “Stack Explained: The Secret Weapon Behind LeetCode Monotonic Problems”</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Thu, 04 Sep 2025 06:19:39 +0000</pubDate>
      <link>https://dev.to/umair01/stack-explained-the-secret-weapon-behind-leetcode-monotonic-problems-2d24</link>
      <guid>https://dev.to/umair01/stack-explained-the-secret-weapon-behind-leetcode-monotonic-problems-2d24</guid>
      <description>&lt;h2&gt;
  
  
  Mastering Stack Data Structure – From Basics to LeetCode Practice
&lt;/h2&gt;

&lt;p&gt;Stacks are one of the most fundamental data structures in computer science and play a critical role in problem-solving, algorithms, and real-world systems. In this post, I’ll walk you through what a stack is, why we need it, important algorithms, and how I’m practicing &lt;strong&gt;LeetCode stack problems&lt;/strong&gt; to master this concept. I’ll also share my GitHub repository link at the end where I upload all my practice codes.&lt;/p&gt;




&lt;h2&gt;
  
  
  📌 What is a Stack?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;stack&lt;/strong&gt; is a linear data structure that follows the principle of &lt;strong&gt;LIFO (Last In, First Out)&lt;/strong&gt;.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imagine stacking plates: you can only take out the plate on the top.
&lt;/li&gt;
&lt;li&gt;The same rule applies to a stack: &lt;strong&gt;Insertions (push) and deletions (pop) happen only at one end (top).&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Basic Operations:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;push(x):&lt;/strong&gt; Insert element &lt;code&gt;x&lt;/code&gt; at the top.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pop():&lt;/strong&gt; Remove the top element.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;top()/peek():&lt;/strong&gt; Get the top element without removing it.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;isEmpty():&lt;/strong&gt; Check if stack is empty.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  ⚡ Why Do We Need a Stack?
&lt;/h2&gt;

&lt;p&gt;Stacks may sound simple, but they are powerful and used everywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Expression Evaluation:&lt;/strong&gt; Balanced Parentheses, Infix → Postfix conversion.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backtracking:&lt;/strong&gt; Undo/redo in editors, recursion call stack.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monotonic Stack Problems:&lt;/strong&gt; Next Greater Element, Stock Span, Daily Temperatures.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System Design:&lt;/strong&gt; Function calls in memory are handled using a &lt;strong&gt;call stack&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔑 Important Stack Problem References
&lt;/h2&gt;

&lt;p&gt;Here are the &lt;strong&gt;must-practice problems&lt;/strong&gt; (with links) that cover all the essential stack patterns:&lt;/p&gt;

&lt;h3&gt;
  
  
  🟢 Easy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/problems/valid-parentheses/" rel="noopener noreferrer"&gt;LeetCode 20 – Valid Parentheses&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/problems/min-stack/" rel="noopener noreferrer"&gt;LeetCode 155 – Min Stack&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/implement-stack-using-queues/" rel="noopener noreferrer"&gt;LeetCode 225 – Implement Stack using Queues&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🟡 Medium
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/problems/next-greater-element-i/" rel="noopener noreferrer"&gt;LeetCode 496 – Next Greater Element I&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/problems/next-greater-element-ii/" rel="noopener noreferrer"&gt;LeetCode 503 – Next Greater Element II&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/problems/daily-temperatures/" rel="noopener noreferrer"&gt;LeetCode 739 – Daily Temperatures&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/online-stock-span/" rel="noopener noreferrer"&gt;LeetCode 901 – Online Stock Span&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔴 Hard
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/problems/largest-rectangle-in-histogram/" rel="noopener noreferrer"&gt;LeetCode 84 – Largest Rectangle in Histogram&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/problems/maximal-rectangle/" rel="noopener noreferrer"&gt;LeetCode 85 – Maximal Rectangle&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⏱️ Time &amp;amp; Space Complexity Analysis
&lt;/h2&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;Time Complexity&lt;/th&gt;
&lt;th&gt;Space Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Push&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pop&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Peek/Top&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Search&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Next Greater Element / Daily Temperatures&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;👉 Most stack-based problems (like NGE, Daily Temperatures) are &lt;strong&gt;O(n)&lt;/strong&gt; because each element is pushed and popped at most once.&lt;/p&gt;




&lt;h2&gt;
  
  
  📚 My LeetCode Practice Plan
&lt;/h2&gt;

&lt;p&gt;To fully master stacks, I’m solving these problems step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Easy Level:&lt;/strong&gt; Warm up with Valid Parentheses, Min Stack.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Medium Level:&lt;/strong&gt; Focus on monotonic stack problems (NGE, Daily Temperatures, Stock Span).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard Level:&lt;/strong&gt; Move on to Histogram and Matrix-based problems.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This ensures I cover &lt;strong&gt;all core algorithms&lt;/strong&gt; involving stacks.&lt;/p&gt;




&lt;h2&gt;
  
  
  📂 GitHub Repository
&lt;/h2&gt;

&lt;p&gt;I’m uploading all my stack solutions (brute force + optimized stack approach) to my GitHub repo.&lt;br&gt;&lt;br&gt;
👉 Check it out here: &lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;My GitHub Repo – Stack Practice&lt;/a&gt;  &lt;/p&gt;




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

&lt;p&gt;Stacks are simple but powerful. By practicing problems like &lt;strong&gt;Next Greater Element, Daily Temperatures, Histogram problems&lt;/strong&gt;, you’ll not only master the stack but also build a strong foundation for advanced algorithms and interview prep.&lt;/p&gt;

&lt;p&gt;💡 My approach: I’m practicing every problem type, pushing solutions to GitHub, and writing blogs to keep track of my journey.  &lt;/p&gt;




&lt;p&gt;🔥 If you’re also practicing, try out these problems, optimize your solutions, and let’s grow together!&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>programming</category>
      <category>cpp</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>🔑 “Mastering Strings in Competitive Programming: Algorithms, Challenges &amp; Solutions”</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Sat, 23 Aug 2025 04:44:08 +0000</pubDate>
      <link>https://dev.to/umair01/mastering-strings-in-competitive-programming-algorithms-challenges-solutions-4df9</link>
      <guid>https://dev.to/umair01/mastering-strings-in-competitive-programming-algorithms-challenges-solutions-4df9</guid>
      <description>&lt;h2&gt;
  
  
  🧵 Strings in DSA – A Complete Guide with Problems, Algorithms &amp;amp; Solutions
&lt;/h2&gt;

&lt;p&gt;Strings are one of the most fundamental yet &lt;strong&gt;tricky data structures&lt;/strong&gt; in programming.&lt;br&gt;&lt;br&gt;
They look simple, but once you dive deeper, you’ll realize they can create some of the &lt;strong&gt;hardest algorithmic challenges&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;In this blog post, I’ll walk you through:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What makes strings tricky
&lt;/li&gt;
&lt;li&gt;Common string-related problems
&lt;/li&gt;
&lt;li&gt;Famous algorithms to solve them
&lt;/li&gt;
&lt;li&gt;Important practice questions
&lt;/li&gt;
&lt;li&gt;📌 And finally, my &lt;strong&gt;GitHub repository&lt;/strong&gt; with full implementations and explanations of all major string algorithms in &lt;strong&gt;C++&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔰 Introduction to Strings
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;string&lt;/strong&gt; is simply a sequence of characters.&lt;br&gt;&lt;br&gt;
But when solving problems, strings become complex because of:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Searching patterns inside a larger string
&lt;/li&gt;
&lt;li&gt;Comparing multiple substrings efficiently
&lt;/li&gt;
&lt;li&gt;Handling overlapping characters
&lt;/li&gt;
&lt;li&gt;Optimizing time complexity for large inputs
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For beginners, brute-force methods often fail due to &lt;strong&gt;TLE (Time Limit Exceeded)&lt;/strong&gt; in coding contests.&lt;br&gt;&lt;br&gt;
That’s where &lt;strong&gt;string algorithms&lt;/strong&gt; come to the rescue. 🚀  &lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ Problems We Face in String Questions
&lt;/h2&gt;

&lt;p&gt;Here are some common categories of string problems you’ll encounter in coding interviews and competitive programming:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Pattern Searching Problems&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: Find if a substring exists in a given string.
&lt;/li&gt;
&lt;li&gt;Naive approach = &lt;code&gt;O(n*m)&lt;/code&gt; (very slow for large strings).
&lt;/li&gt;
&lt;li&gt;✅ Solution: &lt;strong&gt;KMP Algorithm, Rabin-Karp, Z Algorithm&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Prefix &amp;amp; Suffix Problems&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: Find the longest prefix which is also a suffix.
&lt;/li&gt;
&lt;li&gt;✅ Solution: &lt;strong&gt;KMP Prefix Function, Z Algorithm&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Palindrome Problems&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: Longest Palindromic Substring, Palindrome Partitioning.
&lt;/li&gt;
&lt;li&gt;✅ Solution: &lt;strong&gt;Manacher’s Algorithm, Dynamic Programming&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;String Hashing Problems&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: Compare substrings quickly.
&lt;/li&gt;
&lt;li&gt;✅ Solution: &lt;strong&gt;Rabin-Karp, Polynomial Hashing, Rolling Hash&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lexicographical Problems&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: Smallest/Largest rotation of a string.
&lt;/li&gt;
&lt;li&gt;✅ Solution: &lt;strong&gt;Suffix Arrays, Suffix Automaton&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Edit Distance &amp;amp; Transformation Problems&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: Convert one string to another in minimum operations.
&lt;/li&gt;
&lt;li&gt;✅ Solution: &lt;strong&gt;Dynamic Programming (DP), Levenshtein Distance&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Substring Counting &amp;amp; Matching&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: Count distinct substrings, repeated substrings.
&lt;/li&gt;
&lt;li&gt;✅ Solution: &lt;strong&gt;Suffix Array, Trie, Hashing&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🧠 Popular String Algorithms
&lt;/h2&gt;

&lt;p&gt;Here’s a quick list of &lt;strong&gt;must-know algorithms&lt;/strong&gt; for solving string problems efficiently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Naive Pattern Searching&lt;/strong&gt; – baseline method
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sliding Window on Strings&lt;/strong&gt; – efficient substring/window-based problems
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two-Pointer Technique on Strings&lt;/strong&gt; – optimal traversal and comparison
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hashing on Strings&lt;/strong&gt; – rolling hash &amp;amp; substring matching
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;KMP Algorithm (Knuth–Morris–Pratt)&lt;/strong&gt; – efficient pattern searching
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Z Algorithm&lt;/strong&gt; – linear-time substring search
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rabin-Karp Algorithm&lt;/strong&gt; – string matching using hashing
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ve implemented all these algorithms with &lt;strong&gt;step-by-step explanations and C++ code&lt;/strong&gt; in my &lt;strong&gt;GitHub repository&lt;/strong&gt;.  &lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Important String Questions to Practice
&lt;/h2&gt;

&lt;p&gt;Here are some &lt;strong&gt;classic string problems&lt;/strong&gt; (must-do for interviews &amp;amp; CP):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Longest Palindromic Substring
&lt;/li&gt;
&lt;li&gt;Count Distinct Substrings of a String
&lt;/li&gt;
&lt;li&gt;Smallest and Largest Lexicographical Rotation
&lt;/li&gt;
&lt;li&gt;Find All Occurrences of a Pattern in a Text
&lt;/li&gt;
&lt;li&gt;Check if Two Strings are Anagrams
&lt;/li&gt;
&lt;li&gt;Minimum Edit Distance (Levenshtein Distance)
&lt;/li&gt;
&lt;li&gt;Repeated Substring Pattern
&lt;/li&gt;
&lt;li&gt;Longest Prefix Suffix (LPS) Problem
&lt;/li&gt;
&lt;li&gt;Count Number of Palindromic Substrings
&lt;/li&gt;
&lt;li&gt;Find All Substrings of a String (Brute Force + Optimized)
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  📚 Complete Implementations &amp;amp; Explanations
&lt;/h2&gt;

&lt;p&gt;I didn’t want to make a separate post for each algorithm here.&lt;br&gt;&lt;br&gt;
Instead, I’ve compiled all string algorithms, with detailed explanations and &lt;strong&gt;C++ implementations&lt;/strong&gt;, on my GitHub repo.  &lt;/p&gt;

&lt;p&gt;👉 Check out the full collection here:&lt;br&gt;&lt;br&gt;
🔗 &lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;My GitHub Repository on String Algorithms&lt;/a&gt;  &lt;/p&gt;




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

&lt;p&gt;Strings may look easy, but solving string problems efficiently requires &lt;strong&gt;powerful algorithms&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
Once you master KMP, Z, Manacher, Suffix Arrays, and Hashing, you’ll be able to tackle almost any string-related question in coding interviews and competitions.  &lt;/p&gt;

&lt;p&gt;If you’re preparing for &lt;strong&gt;interviews, CP, or a strong DSA foundation&lt;/strong&gt;, bookmark this post and practice the problems listed above.&lt;br&gt;&lt;br&gt;
And whenever you’re stuck, visit my GitHub repo for &lt;strong&gt;ready-to-use C++ code with explanations&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;🚀 Let’s master Strings and crack those coding interviews!  &lt;/p&gt;




</description>
      <category>dsa</category>
      <category>cpp</category>
      <category>programming</category>
      <category>stringalgo</category>
    </item>
    <item>
      <title>Binary Search on Answers — Crack the Hardest Problems in Log Time</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Tue, 12 Aug 2025 05:58:22 +0000</pubDate>
      <link>https://dev.to/umair01/binary-search-on-answers-crack-the-hardest-problems-in-log-time-h5b</link>
      <guid>https://dev.to/umair01/binary-search-on-answers-crack-the-hardest-problems-in-log-time-h5b</guid>
      <description>&lt;h2&gt;
  
  
  🚀 Binary Search on Answers – The Complete Guide with Examples
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;📌 Introduction&lt;/strong&gt;&lt;br&gt;
Binary Search is not just for sorted arrays!&lt;br&gt;
There’s a powerful pattern called "Binary Search on Answers" — where instead of searching inside an array, we search in the range of possible answers to a problem.&lt;/p&gt;

&lt;p&gt;This technique is widely used in optimization problems like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Minimize the maximum workload&lt;/li&gt;
&lt;li&gt;Minimize days to finish a task&lt;/li&gt;
&lt;li&gt;Minimize the speed to complete something&lt;/li&gt;
&lt;li&gt;Allocate tasks with constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your brute force approach is checking answers from 1 to maxPossibleValue, you can often replace it with Binary Search to drastically speed things up.&lt;/p&gt;
&lt;h2&gt;
  
  
  📖 Definition
&lt;/h2&gt;

&lt;p&gt;Binary Search on Answers is an algorithmic technique where:&lt;/p&gt;

&lt;p&gt;You define the search space as all possible values of the answer.&lt;/p&gt;

&lt;p&gt;You check feasibility of a middle value using a helper function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;`If the answer is possible, search the left half (to minimize).&lt;/li&gt;
&lt;li&gt;If the answer is not possible, search the right half (to increase).`&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🪜 Naive Approach (Brute Force)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Iterate over all possible values of the answer.&lt;/li&gt;
&lt;li&gt;For each value, check if it satisfies the problem constraints.&lt;/li&gt;
&lt;li&gt;Take the smallest valid value (or largest if maximizing).&lt;/li&gt;
&lt;li&gt;Drawback: Too slow when the answer space is huge (e.g., 1e9).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  ⚡ Optimized Approach (Binary Search)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Instead of checking every answer, cut the search space in half each time.&lt;/li&gt;
&lt;li&gt;Use a helper function isFeasible() to check if the mid value works.&lt;/li&gt;
&lt;li&gt;Achieves O(log(maxPossibleValue) × N) time complexity.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  ⏳ Time &amp;amp; Space Complexity
&lt;/h2&gt;

&lt;p&gt;Brute Force  O(N × maxValue)   O(1)&lt;br&gt;
Optimized (BS)   O(N × log(maxValue))  O(1)&lt;/p&gt;
&lt;h2&gt;
  
  
  💡 Example Problem – Koko Eating Bananas
&lt;/h2&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Koko loves to eat bananas. She can decide her eating speed k bananas/hour.&lt;br&gt;
Given piles and h, find the minimum k to eat all bananas before guards return.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;🛠 Brute Force Approach&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;cpp

#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

// Check if Koko can eat all bananas at speed k in h hours
bool canEatAll(vector&amp;lt;int&amp;gt;&amp;amp; piles, int k, int h) {
    long long totalHours = 0;
    for (int bananas : piles) {
        totalHours += (bananas + k - 1) / k; // ceil division
    }
    return totalHours &amp;lt;= h;
}

int minEatingSpeedBruteForce(vector&amp;lt;int&amp;gt;&amp;amp; piles, int h) {
    int maxPile = *max_element(piles.begin(), piles.end());
    for (int k = 1; k &amp;lt;= maxPile; k++) {
        if (canEatAll(piles, k, h)) {
            return k;
        }
    }
    return maxPile;
}

int main() {
    vector&amp;lt;int&amp;gt; piles = {3, 6, 7, 11};
    int h = 8;
    cout &amp;lt;&amp;lt; "Brute Force Result: " &amp;lt;&amp;lt; minEatingSpeedBruteForce(piles, h) &amp;lt;&amp;lt; endl;
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⚡ Optimized Approach (Binary Search)
&lt;/h2&gt;



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

#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

bool canEatAll(vector&amp;lt;int&amp;gt;&amp;amp; piles, int k, int h) {
    long long totalHours = 0;
    for (int bananas : piles) {
        totalHours += (bananas + k - 1) / k;
    }
    return totalHours &amp;lt;= h;
}

int minEatingSpeedOptimized(vector&amp;lt;int&amp;gt;&amp;amp; piles, int h) {
    int low = 1, high = *max_element(piles.begin(), piles.end());
    while (low &amp;lt; high) {
        int mid = low + (high - low) / 2;
        if (canEatAll(piles, mid, h)) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }
    return low;
}

int main() {
    vector&amp;lt;int&amp;gt; piles = {3, 6, 7, 11};
    int h = 8;
    cout &amp;lt;&amp;lt; "Optimized Result: " &amp;lt;&amp;lt; minEatingSpeedOptimized(piles, h) &amp;lt;&amp;lt; endl;
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;📝 Step-by-Step Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define Search Space: Eating speed ranges from 1 to max(piles).&lt;/li&gt;
&lt;li&gt;Check Feasibility: If she can finish at speed mid, try smaller speed.&lt;/li&gt;
&lt;li&gt;Shrink Search Space: Binary search reduces the range quickly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🔥 Important Practice Questions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Koko Eating Bananas – LeetCode #875&lt;/li&gt;
&lt;li&gt;Capacity to Ship Packages Within D Days – LeetCode #1011&lt;/li&gt;
&lt;li&gt;Minimum Number of Days to Make m Bouquets – LeetCode #1482&lt;/li&gt;
&lt;li&gt;Split Array Largest Sum – LeetCode #410&lt;/li&gt;
&lt;li&gt;Book Allocation Problem – GFG&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🤝 My Open Source Contribution
&lt;/h2&gt;

&lt;p&gt;I’ve implemented these problems in my open-source GitHub repository:&lt;br&gt;
📂 GitHub Repo: &lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;Check Out.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Use binary search on answers when brute force is iterating over possible values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always identify the search space &amp;amp; feasibility function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s a game changer for optimization problems.`&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>programming</category>
      <category>dsa</category>
      <category>cpp</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>The Logic Behind 0s, 1s, and 2s (DNF)</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Sun, 03 Aug 2025 06:07:10 +0000</pubDate>
      <link>https://dev.to/umair01/the-logic-behind-0s-1s-and-2s-dnf-50ae</link>
      <guid>https://dev.to/umair01/the-logic-behind-0s-1s-and-2s-dnf-50ae</guid>
      <description>&lt;h2&gt;
  
  
  🚩 Mastering the Dutch National Flag Algorithm in C++
&lt;/h2&gt;

&lt;p&gt;Sorting 0s, 1s, and 2s in a single pass? That's the power of the &lt;strong&gt;Dutch National Flag Algorithm&lt;/strong&gt;, an elegant algorithm designed by computer scientist &lt;strong&gt;Edsger Dijkstra&lt;/strong&gt;. It's an essential technique in your DSA toolbox!&lt;/p&gt;




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

&lt;p&gt;You're given an array consisting only of 0s, 1s, and 2s. Your task is to sort the array &lt;strong&gt;in-place&lt;/strong&gt; in a single pass so that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All 0s appear first&lt;/li&gt;
&lt;li&gt;All 1s appear next&lt;/li&gt;
&lt;li&gt;All 2s appear last&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;: &lt;code&gt;nums = [2, 0, 2, 1, 1, 0]&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Output&lt;/strong&gt;: &lt;code&gt;nums = [0, 0, 1, 1, 2, 2]&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🤔 Real-Life Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine you have a bucket of &lt;strong&gt;red&lt;/strong&gt;, &lt;strong&gt;white&lt;/strong&gt;, and &lt;strong&gt;blue&lt;/strong&gt; balls. You want to group them together in the order of colors — this is exactly what the Dutch National Flag problem is about!&lt;/p&gt;




&lt;h2&gt;
  
  
  📚 Understanding the Concept
&lt;/h2&gt;

&lt;p&gt;The Dutch National Flag problem can be solved by maintaining &lt;strong&gt;three pointers&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;low&lt;/code&gt;: to track the position where the next 0 should be placed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mid&lt;/code&gt;: the current element being evaluated.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;high&lt;/code&gt;: to track the position where the next 2 should go.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swap 0s to the beginning.&lt;/li&gt;
&lt;li&gt;Leave 1s in the middle.&lt;/li&gt;
&lt;li&gt;Swap 2s to the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🧠 Table to Understand Swapping:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index&lt;/th&gt;
&lt;th&gt;Before Action&lt;/th&gt;
&lt;th&gt;After Action (if nums[mid] == 0)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;swap(nums[low], nums[mid])&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;++low, ++mid&lt;/td&gt;
&lt;td&gt;++low, ++mid&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🌀 Naive Approach
&lt;/h2&gt;

&lt;p&gt;Use a counting sort-like method.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧾 Steps:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Count the number of 0s, 1s, and 2s.&lt;/li&gt;
&lt;li&gt;Overwrite the original array with counted values.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  ⏱ Time: O(n)
&lt;/h3&gt;

&lt;h3&gt;
  
  
  🧠 Space: O(1)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cpp
void sortColors(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
    int zero = 0, one = 0, two = 0;
    for (int num : nums) {
        if (num == 0) zero++;
        else if (num == 1) one++;
        else two++;
    }

    int i = 0;
    while (zero--) nums[i++] = 0;
    while (one--) nums[i++] = 1;
    while (two--) nums[i++] = 2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⚡ Optimized One-Pass Approach (Dutch National Flag Algorithm)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;🚀 Algorithm Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initialize low = 0, mid = 0, and high = n - 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While mid &amp;lt;= high, check:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If nums[mid] == 0: swap with nums[low], increment low and mid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If nums[mid] == 1: just increment mid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If nums[mid] == 2: swap with nums[high], decrement high.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;✅ Time Complexity: O(n)&lt;br&gt;
✅ Space Complexity: O(1)&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  💻 Code:
&lt;/h2&gt;



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

void sortColors(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
    int low = 0, mid = 0, high = nums.size() - 1;

    while (mid &amp;lt;= high) {
        if (nums[mid] == 0) {
            swap(nums[low], nums[mid]);
            low++; mid++;
        }
        else if (nums[mid] == 1) {
            mid++;
        }
        else {
            swap(nums[mid], nums[high]);
            high--;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧪 Dry Run Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [2, 0, 2, 1, 1, 0]
Initial: low = 0, mid = 0, high = 5

Step 1: swap(nums[0], nums[5]) -&amp;gt; [0, 0, 2, 1, 1, 2]
low = 0, mid = 0, high = 4

Step 2: nums[0] == 0 =&amp;gt; swap(nums[0], nums[0]) =&amp;gt; low++, mid++`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Final: [0, 0, 1, 1, 2, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔗 Related Problems to Practice
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sort Colors
&lt;/li&gt;
&lt;li&gt;Sort Array by Parity
&lt;/li&gt;
&lt;li&gt;Wiggle Sort
&lt;/li&gt;
&lt;li&gt;Rainbow Sort
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎯 When to Use Dutch National Flag?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When the array contains 3 distinct categories or values.&lt;/li&gt;
&lt;li&gt;You want to sort in-place with constant space.&lt;/li&gt;
&lt;li&gt;Common in segregation problems, such as even/odd, 0s/1s, red/blue/green, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🚀 Open Source Contribution
&lt;/h2&gt;

&lt;p&gt;I'm solving DSA problems in C++ and uploading them algorithm-wise to GitHub.&lt;br&gt;
Feel free to check it out, use the solutions, or contribute!&lt;/p&gt;

&lt;p&gt;👉 📁 DSA-in-C++ &lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>dsa</category>
      <category>productivity</category>
      <category>dutchflag</category>
    </item>
    <item>
      <title>What I Wish I Knew Before Learning the Two Pointer Algorithm</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Sat, 02 Aug 2025 07:04:47 +0000</pubDate>
      <link>https://dev.to/umair01/what-i-wish-i-knew-before-learning-the-two-pointer-algorithm-2gbk</link>
      <guid>https://dev.to/umair01/what-i-wish-i-knew-before-learning-the-two-pointer-algorithm-2gbk</guid>
      <description>&lt;h2&gt;
  
  
  🚀 Mastering the Two Pointers Technique in DSA
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Two Pointers&lt;/strong&gt; technique is one of the most efficient and elegant solutions for solving array and string problems that involve searching, partitioning, or comparison. This guide will walk you through the complete concept of the Two Pointers technique, its motivation, real-world applications, variations, problem patterns, and code examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔍 What is the Two Pointers Technique?
&lt;/h2&gt;

&lt;p&gt;The Two Pointers technique involves using two variables (usually indices) that move through the data structure (like an array or string) in a coordinated way to solve a problem in linear or near-linear time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Usually applied to &lt;strong&gt;sorted arrays&lt;/strong&gt; or &lt;strong&gt;strings&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Used to avoid nested loops and reduce time complexity from &lt;code&gt;O(n^2)&lt;/code&gt; to &lt;code&gt;O(n)&lt;/code&gt; or &lt;code&gt;O(n log n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Pointers can move:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From both ends toward the center (inward)&lt;/li&gt;
&lt;li&gt;From start to end (one slow, one fast)&lt;/li&gt;
&lt;li&gt;Sliding over a fixed or dynamic window&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Motivation
&lt;/h2&gt;

&lt;p&gt;Two Pointers is widely used because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It reduces brute-force complexity&lt;/li&gt;
&lt;li&gt;It is elegant, easy to debug, and memory efficient&lt;/li&gt;
&lt;li&gt;It covers core patterns for sliding window, partitioning, merging, etc.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔧 Variations of Two Pointers
&lt;/h2&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;Description&lt;/th&gt;
&lt;th&gt;Examples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Opposite Ends&lt;/td&gt;
&lt;td&gt;One pointer from start, one from end&lt;/td&gt;
&lt;td&gt;Container With Most Water, Two Sum II&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same Direction&lt;/td&gt;
&lt;td&gt;Both pointers start from one end&lt;/td&gt;
&lt;td&gt;Remove Duplicates, Move Zeroes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sliding Window&lt;/td&gt;
&lt;td&gt;Two pointers define window&lt;/td&gt;
&lt;td&gt;Longest Substring Without Repeating Characters&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  📚 Common Problem Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Two Sum II (Sorted Array)&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;twoSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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="p"&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;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&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;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&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;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;strong&gt;Container With Most Water&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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;0&lt;/span&gt;&lt;span class="p"&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;height&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maxWater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&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="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;maxWater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxWater&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxWater&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;strong&gt;Move Zeroes&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;moveZeroes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&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;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. &lt;strong&gt;4Sum Problem (Nested Two Pointers)&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fourSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="p"&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;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&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="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&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="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&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;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;]});&lt;/span&gt;
                    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧠 Pattern Recognition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Partitioning Arrays&lt;/strong&gt;: Move all negative/positive/zero elements to one side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sorting Problems&lt;/strong&gt;: Merge sorted arrays, sort colors (Dutch National Flag).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Searching Problems&lt;/strong&gt;: Two Sum, 3Sum, 4Sum, Closest Sum.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Window-based problems&lt;/strong&gt;: Longest substring with no repeating characters.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🌎 Real-World Applications
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Network Buffers&lt;/strong&gt;: Optimizing streaming packet flow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image Processing&lt;/strong&gt;: Windowed filtering, edge detection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bioinformatics&lt;/strong&gt;: DNA strand comparison.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Finance&lt;/strong&gt;: Max profit window in stock prices.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔥 Bonus: Open Source Contributions
&lt;/h2&gt;

&lt;p&gt;If you're looking to contribute to open source using Two Pointers logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explore LeetCode-style repositories like &lt;a href="https://github.com/TheAlgorithms/C-Plus-Plus" rel="noopener noreferrer"&gt;The-Algorithms/C++&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Improve test coverage or write explanations in &lt;a href="https://takeuforward.org/interviews/strivers-sde-sheet-top-coding-interview-problems/" rel="noopener noreferrer"&gt;Striver’s SDE Sheet&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Participate in community-driven DSA content at &lt;a href="https://codeforces.com/" rel="noopener noreferrer"&gt;Codeforces&lt;/a&gt; or &lt;a href="https://www.codingninjas.com/studio" rel="noopener noreferrer"&gt;CodeStudio&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧑‍💻 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Two Pointers is a cornerstone of your DSA foundation. Once you master it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You reduce unnecessary complexity.&lt;/li&gt;
&lt;li&gt;Improve runtime drastically.&lt;/li&gt;
&lt;li&gt;Get closer to acing coding interviews at FAANG and top startups.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;"Elegance in problem-solving often starts with two simple pointers."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Open Conntribution
&lt;/h2&gt;

&lt;p&gt;Check out my &lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;Github Repo&lt;/a&gt; &lt;/p&gt;




&lt;p&gt;Happy Coding 👨‍💻🎯&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>programming</category>
      <category>cpp</category>
      <category>2pointers</category>
    </item>
    <item>
      <title>From Brute Force to Kadane’s: Solving the Maximum Subarray Problem Efficiently</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Thu, 31 Jul 2025 07:07:46 +0000</pubDate>
      <link>https://dev.to/umair01/from-brute-force-to-kadanes-solving-the-maximum-subarray-problem-efficiently-37eo</link>
      <guid>https://dev.to/umair01/from-brute-force-to-kadanes-solving-the-maximum-subarray-problem-efficiently-37eo</guid>
      <description>&lt;h2&gt;
  
  
  🚀 Mastering Kadane's Algorithm: The Ultimate Guide to Maximum Subarray Sum
&lt;/h2&gt;

&lt;p&gt;Kadane's Algorithm is one of the most elegant and widely asked &lt;strong&gt;Dynamic Programming&lt;/strong&gt; techniques in coding interviews and DSA contests. If you're tackling problems involving &lt;strong&gt;maximum sum of contiguous subarrays&lt;/strong&gt;, then this is a must-have in your toolbox.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 What is the Problem?
&lt;/h2&gt;

&lt;p&gt;You're given an array of integers (which may contain both positive and negative numbers). You need to find the &lt;strong&gt;maximum sum of any contiguous subarray&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Real-World Analogy
&lt;/h2&gt;

&lt;p&gt;Imagine you're tracking your profit/loss daily in a month. You want to find the &lt;strong&gt;most profitable streak&lt;/strong&gt; of consecutive days. Some days might have loss (negative), some profit (positive), but you must find the &lt;strong&gt;contiguous&lt;/strong&gt; stretch that gives you the &lt;strong&gt;maximum net gain&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  📊 Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔍 Step-by-Step Intuition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Start with a sum of 0 and a variable to track the maximum sum so far.&lt;/li&gt;
&lt;li&gt;Iterate through each number:&lt;/li&gt;
&lt;li&gt;Add it to your current sum.&lt;/li&gt;
&lt;li&gt;If the current sum is greater than the max, update max.&lt;/li&gt;
&lt;li&gt;If current sum becomes negative, reset it to 0 (because a negative sum won't help going forward).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🐢 Brute-Force Approach (Naive Method)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cpp
// Time Complexity: O(n^2)
int maxSubArray(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
    int n = nums.size();
    int maxSum = INT_MIN;

    for (int i = 0; i &amp;lt; n; ++i) {
        int currSum = 0;
        for (int j = i; j &amp;lt; n; ++j) {
            currSum += nums[j];
            maxSum = max(maxSum, currSum);
        }
    }

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

&lt;/div&gt;



&lt;p&gt;❌ Inefficient for large arrays.&lt;br&gt;
✅ Good for understanding the concept.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚡ Optimized Approach: Kadane’s Algorithm
&lt;/h2&gt;



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

// Time Complexity: O(n)
// Space Complexity: O(1)

int maxSubArray(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
    int currSum = 0, maxSum = INT_MIN;

    for (int i = 0; i &amp;lt; nums.size(); i++) {
        currSum += nums[i];
        maxSum = max(maxSum, currSum);

        if (currSum &amp;lt; 0)
            currSum = 0;
    }

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧪 Practice Examples
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Try running the algorithm on:

nums = [5,4,-1,7,8] → Output: 23

nums = [-1,-2,-3,-4] → Output: -1

nums = [1] → Output: 1

nums = [8, -19, 5, -4, 20] → Output: 21
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔁 Related LeetCode Problems to Practice
&lt;/h2&gt;

&lt;p&gt;** Problem**    &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maximum Subarray
&lt;/li&gt;
&lt;li&gt;Maximum Product Subarray
&lt;/li&gt;
&lt;li&gt;Maximum Sum Circular Subarray &lt;/li&gt;
&lt;li&gt;Contiguous Array&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💼 Add It to Your Portfolio
&lt;/h2&gt;

&lt;p&gt;This is one of the most interview-favorite problems asked by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google&lt;/li&gt;
&lt;li&gt;Amazon&lt;/li&gt;
&lt;li&gt;Microsoft&lt;/li&gt;
&lt;li&gt;Netflix&lt;/li&gt;
&lt;li&gt;Adobe&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💻 Open Source GitHub Solutions
&lt;/h2&gt;

&lt;p&gt;🔗 Check out my full Kadane’s Algorithm solution with explanations here:&lt;br&gt;
👉&lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt; GitHub Repository&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Kadane’s Algorithm is your go-to solution when you're dealing with subarrays and maximum sums. It’s blazing fast (O(n)), elegant, and powerful. Practice the problems, understand the variations, and you'll master it in no time!&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>programming</category>
      <category>productivity</category>
      <category>kadanealgo</category>
    </item>
    <item>
      <title>🧠 Sliding Window Pattern: The Secret Sauce for Efficient Algorithms</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Tue, 29 Jul 2025 07:25:13 +0000</pubDate>
      <link>https://dev.to/umair01/sliding-window-pattern-the-secret-sauce-for-efficient-algorithms-2fd4</link>
      <guid>https://dev.to/umair01/sliding-window-pattern-the-secret-sauce-for-efficient-algorithms-2fd4</guid>
      <description>&lt;h2&gt;
  
  
  🚀 Mastering the Sliding Window Technique in C++: Crack DSA Like a Pro!
&lt;/h2&gt;

&lt;p&gt;When you're tackling arrays or strings in competitive programming or coding interviews, &lt;strong&gt;efficiency&lt;/strong&gt; is king. That’s where the &lt;strong&gt;Sliding Window&lt;/strong&gt; technique comes in — a game-changing approach that helps you move from brute-force to optimized solutions.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll dive deep into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💡 What is Sliding Window?&lt;/li&gt;
&lt;li&gt;🧠 Why use it?&lt;/li&gt;
&lt;li&gt;🧱 Brute Force vs Optimized Approaches&lt;/li&gt;
&lt;li&gt;📊 Time &amp;amp; Space Complexities&lt;/li&gt;
&lt;li&gt;🧪 Real-world Examples&lt;/li&gt;
&lt;li&gt;🔗 My GitHub Repository for Open Source Contribution&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 What is Sliding Window?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Sliding Window&lt;/strong&gt; is a technique for solving problems involving linear data structures like arrays and strings. It involves creating a &lt;strong&gt;window&lt;/strong&gt; (usually a subarray or substring) and &lt;strong&gt;moving it across the input&lt;/strong&gt; to find a solution — without recalculating everything inside the window.&lt;/p&gt;




&lt;h2&gt;
  
  
  🤕 The Brute Force Way (Naive Approach)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Problem Example:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Find the maximum sum of a subarray of size &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Naive Approach (O(n*k)):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cpp
int maxSumBruteForce(vector&amp;lt;int&amp;gt;&amp;amp; nums, int k) {
    int n = nums.size();
    int maxSum = INT_MIN;
    for (int i = 0; i &amp;lt;= n - k; i++) {
        int sum = 0;
        for (int j = 0; j &amp;lt; k; j++) {
            sum += nums[i + j];
        }
        maxSum = max(maxSum, sum);
    }
    return maxSum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity: O(n*k)&lt;br&gt;
Space Complexity: O(1)&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 Optimized Sliding Window Approach
&lt;/h2&gt;



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

int maxSumSlidingWindow(vector&amp;lt;int&amp;gt;&amp;amp; nums, int k) {
    int windowSum = 0, maxSum = INT_MIN;
    for (int i = 0; i &amp;lt; k; i++) windowSum += nums[i];
    maxSum = windowSum;

    for (int i = k; i &amp;lt; nums.size(); i++) {
        windowSum += nums[i] - nums[i - k];
        maxSum = max(maxSum, windowSum);
    }
    return maxSum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;This approach avoids recalculating sums by adding the next element and removing the first element of the previous window.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧪 Real Interview Examples Using Sliding Window
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;🔥 Maximum Sum Subarray of Size K&lt;/li&gt;
&lt;li&gt;🍎 Fruit Into Baskets&lt;/li&gt;
&lt;li&gt;🥇 Longest Subarray of 1s After Deleting One Element&lt;/li&gt;
&lt;li&gt;🧮 Count Number of Nice Subarrays&lt;/li&gt;
&lt;li&gt;💼 Minimum Size Subarray Sum&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  📚 Use-Cases of Sliding Window
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Find the max/min/average of a subarray&lt;/li&gt;
&lt;li&gt;Count distinct elements in a subarray&lt;/li&gt;
&lt;li&gt;Longest substring with at most k distinct characters&lt;/li&gt;
&lt;li&gt;Smallest window that satisfies a condition&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ⚙️ Template Code (Variable-Size Window):
&lt;/h2&gt;



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

int solve(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
    int i = 0, j = 0, answer = 0;
    unordered_map&amp;lt;int, int&amp;gt; freq;

    while (j &amp;lt; nums.size()) {
        freq[nums[j]]++;

        // If condition violated
        while (freq.size() &amp;gt; k) {
            freq[nums[i]]--;
            if (freq[nums[i]] == 0) freq.erase(nums[i]);
            i++;
        }

        answer = max(answer, j - i + 1);
        j++;
    }

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  👨‍💻 Open Source Contribution
&lt;/h2&gt;

&lt;p&gt;All of my DSA solutions using Sliding Window (including the LeetCode/Striver/Nudecode-150 ones) are available in my GitHub repo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;🔗 GitHub Repository:&lt;/a&gt;** 👉 Click Here to Explore My Solutions&lt;/p&gt;

&lt;p&gt;Make sure to ⭐ star the repo and follow me for more updates!**&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>programming</category>
      <category>cpp</category>
      <category>slidingwindow</category>
    </item>
    <item>
      <title>⏱️ O(N) to O(1): How Prefix Sum Will Change Your Code Forever</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Tue, 29 Jul 2025 07:16:14 +0000</pubDate>
      <link>https://dev.to/umair01/on-to-o1-how-prefix-sum-will-change-your-code-forever-2l33</link>
      <guid>https://dev.to/umair01/on-to-o1-how-prefix-sum-will-change-your-code-forever-2l33</guid>
      <description>&lt;h2&gt;
  
  
  🚀 Mastering Prefix Sum in C++: From Naive to Optimized
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Prefix Sum&lt;/strong&gt; technique is one of the most powerful tools in array-based problems. It reduces time complexity significantly and is widely used in &lt;strong&gt;competitive programming&lt;/strong&gt;, &lt;strong&gt;interviews&lt;/strong&gt;, and &lt;strong&gt;DSA&lt;/strong&gt; questions.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 What is Prefix Sum?
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;prefix sum&lt;/strong&gt; of an array is a new array &lt;code&gt;prefix[]&lt;/code&gt; where:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;prefix[i] = arr[0] + arr[1] + ... + arr[i]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple terms, each index holds the &lt;strong&gt;cumulative sum&lt;/strong&gt; from the start to that point.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Why Use Prefix Sum?
&lt;/h2&gt;

&lt;p&gt;Prefix Sum is useful when you’re asked to compute &lt;strong&gt;sum of elements in a subarray multiple times&lt;/strong&gt; efficiently.&lt;/p&gt;

&lt;p&gt;Instead of recalculating the sum for every query, you &lt;strong&gt;precompute&lt;/strong&gt; the prefix sum and &lt;strong&gt;answer each query in O(1)&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🐢 Brute Force Approach (Naive)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ❌ Time Complexity: O(N * Q)
&lt;/h3&gt;

&lt;p&gt;For each query, you iterate over the subarray and calculate the sum.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cpp
int rangeSum(vector&amp;lt;int&amp;gt;&amp;amp; arr, int l, int r) {
    int sum = 0;
    for (int i = l; i &amp;lt;= r; i++) {
        sum += arr[i];
    }
    return sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⚡ Optimized Approach using Prefix Sum
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;✅ Time Complexity&lt;/strong&gt;: O(N) preprocessing + O(1) per query&lt;br&gt;
&lt;strong&gt;📘 Formula:&lt;/strong&gt;&lt;br&gt;
`prefix[0] = arr[0]&lt;/p&gt;

&lt;p&gt;prefix[i] = prefix[i-1] + arr[i]&lt;/p&gt;

&lt;p&gt;sum of range (l, r) = prefix[r] - prefix[l-1]`&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
cpp
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
using namespace std;

class PrefixSum {
public:
    vector&amp;lt;int&amp;gt; computePrefixSum(vector&amp;lt;int&amp;gt;&amp;amp; arr) {
        vector&amp;lt;int&amp;gt; prefix(arr.size());
        prefix[0] = arr[0];

        for (int i = 1; i &amp;lt; arr.size(); i++) {
            prefix[i] = prefix[i - 1] + arr[i];
        }

        return prefix;
    }

    int rangeSum(vector&amp;lt;int&amp;gt;&amp;amp; prefix, int l, int r) {
        if (l == 0) return prefix[r];
        return prefix[r] - prefix[l - 1];
    }
};

int main() {
    vector&amp;lt;int&amp;gt; arr = {2, 4, 6, 8, 10};
    PrefixSum ps;
    vector&amp;lt;int&amp;gt; prefix = ps.computePrefixSum(arr);

    cout &amp;lt;&amp;lt; "Sum of elements from index 1 to 3: "
         &amp;lt;&amp;lt; ps.rangeSum(prefix, 1, 3) &amp;lt;&amp;lt; endl; // Output: 18

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  📊 Time &amp;amp; Space Complexity
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Operation Time Complexity Space Complexity&lt;/strong&gt;&lt;br&gt;
Brute Force Query   O(N)    O(1)&lt;br&gt;
Prefix Preprocess   O(N)    O(N)&lt;br&gt;
Prefix Query    O(1)    O(N)&lt;/p&gt;

&lt;h2&gt;
  
  
  💎 Real Use Cases of Prefix Sum
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Count number of subarrays with a given sum&lt;/li&gt;
&lt;li&gt;Range sum queries&lt;/li&gt;
&lt;li&gt;2D Prefix Sum (matrix)&lt;/li&gt;
&lt;li&gt;Sliding window enhancements&lt;/li&gt;
&lt;li&gt;Difference arrays (range update queries)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🧪 Example Problem
&lt;/h2&gt;

&lt;p&gt;Problem: Given an array arr[], and multiple queries of the form (L, R), compute the sum of elements from index L to R.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Input:&lt;br&gt;
arr = [1, 3, 2, 5, 4]&lt;br&gt;
Queries:&lt;br&gt;
(1, 3) =&amp;gt; 3+2+5 = 10&lt;br&gt;
(0, 4) =&amp;gt; 1+3+2+5+4 = 15&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using prefix sum: Precompute prefix array once, and answer in O(1) for each query.&lt;/p&gt;

&lt;p&gt;📦 GitHub Repository&lt;br&gt;
🌟 Check out the full repository with all problems solved using Prefix Sum in C++ here:&lt;br&gt;
👉 &lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;My GitHub DSA Repository - Prefix Sum&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>cpp</category>
      <category>programming</category>
      <category>prefixsum</category>
    </item>
    <item>
      <title>Maximum sum rectangle in a 2D matrix.</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Fri, 25 Jul 2025 07:55:48 +0000</pubDate>
      <link>https://dev.to/umair01/maximum-sum-rectangle-in-a-2d-matrix-3ed9</link>
      <guid>https://dev.to/umair01/maximum-sum-rectangle-in-a-2d-matrix-3ed9</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given a 2D matrix of integers, find the maximum sum rectangle within the matrix using the prefix sum technique.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;matrix = [&lt;br&gt;
    [ 1,  2, -1, -4, -20],&lt;br&gt;
    [-8, -3,  4,  2,   1],&lt;br&gt;
    [ 3,  8, 10,  1,   3],&lt;br&gt;
    [-4, -1,  1,  7,  -6]&lt;br&gt;
]&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;29&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach (2D Prefix Sum + Submatrix Sum Calculation)
&lt;/h2&gt;

&lt;p&gt;Key Idea:&lt;br&gt;
Construct a 2D prefix sum matrix, where prefix[i][j] represents the sum of all elements in the submatrix from (0,0) to (i,j).&lt;/p&gt;

&lt;p&gt;For any submatrix (r1, c1) to (r2, c2), the sum can be found in O(1) using the prefix matrix:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;submatrix_sum = prefix[r2][c2] &lt;br&gt;
                - prefix[r1-1][c2] &lt;br&gt;
                - prefix[r2][c1-1] &lt;br&gt;
                + prefix[r1-1][c1-1]&lt;/code&gt;&lt;br&gt;
Iterate over all pairs of (r1, r2) and (c1, c2) to find the maximum sum submatrix.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Build a 2D prefix sum matrix in O(rows × cols). &lt;/li&gt;
&lt;li&gt; For every pair of top-left (r1, c1) and bottom-right (r2, c2) corners, calculate the submatrix sum using the prefix matrix.&lt;/li&gt;
&lt;li&gt; Keep track of the maximum sum found.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  C++ Code (Prefix Sum)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

int maxSumRectanglePrefix(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; matrix) {
    int rows = matrix.size();
    int cols = matrix[0].size();

    // Build prefix sum matrix
    vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; prefix(rows + 1, vector&amp;lt;int&amp;gt;(cols + 1, 0));
    for (int i = 1; i &amp;lt;= rows; i++) {
        for (int j = 1; j &amp;lt;= cols; j++) {
            prefix[i][j] = matrix[i-1][j-1] 
                         + prefix[i-1][j] 
                         + prefix[i][j-1] 
                         - prefix[i-1][j-1];
        }
    }

    int maxSum = INT_MIN;

    // Iterate over all submatrices
    for (int r1 = 1; r1 &amp;lt;= rows; r1++) {
        for (int c1 = 1; c1 &amp;lt;= cols; c1++) {
            for (int r2 = r1; r2 &amp;lt;= rows; r2++) {
                for (int c2 = c1; c2 &amp;lt;= cols; c2++) {
                    int currSum = prefix[r2][c2]
                                - prefix[r1-1][c2]
                                - prefix[r2][c1-1]
                                + prefix[r1-1][c1-1];
                    maxSum = max(maxSum, currSum);
                }
            }
        }
    }
    return maxSum;
}

int main() {
    vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; matrix = {
        { 1,  2, -1, -4, -20},
        {-8, -3,  4,  2,   1},
        { 3,  8, 10,  1,   3},
        {-4, -1,  1,  7,  -6}
    };
    cout &amp;lt;&amp;lt; "Maximum Sum Rectangle = " &amp;lt;&amp;lt; maxSumRectanglePrefix(matrix) &amp;lt;&amp;lt; endl;
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Time Complexity: O(rows² × cols²) (because we consider all submatrices).&lt;/p&gt;

&lt;p&gt;Space Complexity: O(rows × cols) (for the prefix matrix).&lt;/p&gt;

&lt;h2&gt;
  
  
  Open Source Contribution
&lt;/h2&gt;

&lt;p&gt;I am building an open-source repository of DSA solutions in C++, organized algorithm-wise for easier learning.&lt;br&gt;
Feel free to explore, star ⭐, and contribute to my repository:&lt;br&gt;
&lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;👉 DSA in C++ – GitHub Repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>dsa</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>LC:370 Range Addition.</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Fri, 25 Jul 2025 07:44:58 +0000</pubDate>
      <link>https://dev.to/umair01/lc370-range-addition-3bc8</link>
      <guid>https://dev.to/umair01/lc370-range-addition-3bc8</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given an integer n and an array of m update operations, where each update operation is represented as a triplet:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[startIndex, endIndex, inc].&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Initially, you have an array nums of length n filled with 0.&lt;/p&gt;

&lt;p&gt;For each update [startIndex, endIndex, inc], add inc to each element of nums in the range startIndex &amp;lt;= i &amp;lt;= endIndex.&lt;/p&gt;

&lt;p&gt;Return the final nums array after applying all the operations.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;n = 5  &lt;br&gt;
updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]]&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;[-2, 0, 3, 5, 3]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Explanation:&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
`Start with [0, 0, 0, 0, 0]&lt;/p&gt;

&lt;p&gt;Apply [1, 3, 2] → [0, 2, 2, 2, 0]&lt;/p&gt;

&lt;p&gt;Apply [2, 4, 3] → [0, 2, 5, 5, 3]&lt;/p&gt;

&lt;p&gt;Apply [0, 2, -2] → [-2, 0, 3, 5, 3]`&lt;/p&gt;
&lt;h2&gt;
  
  
  Approach (Difference Array + Prefix Sum)
&lt;/h2&gt;

&lt;p&gt;A naive approach would update each element in [startIndex..endIndex] individually for every update, resulting in O(m * n) time.&lt;/p&gt;

&lt;p&gt;Instead, we use a Difference Array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For an update [l, r, inc], we do:

diff[l] += inc

diff[r + 1] -= inc (if r + 1 &amp;lt; n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After all updates, we take the prefix sum of the diff array to construct the final nums.&lt;/p&gt;

&lt;p&gt;This reduces the complexity to&lt;code&gt;O(n + m).&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  C++ Solution Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; getModifiedArray(int n, vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; updates) {
    vector&amp;lt;int&amp;gt; diff(n, 0);

    // Apply difference array updates
    for (auto&amp;amp; u : updates) {
        int start = u[0], end = u[1], val = u[2];
        diff[start] += val;
        if (end + 1 &amp;lt; n) diff[end + 1] -= val;
    }

    // Build final array by taking prefix sums
    vector&amp;lt;int&amp;gt; result(n, 0);
    int runningSum = 0;
    for (int i = 0; i &amp;lt; n; i++) {
        runningSum += diff[i];
        result[i] = runningSum;
    }
    return result;
}

int main() {
    int n = 5;
    vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; updates = {{1, 3, 2}, {2, 4, 3}, {0, 2, -2}};
    vector&amp;lt;int&amp;gt; res = getModifiedArray(n, updates);

    cout &amp;lt;&amp;lt; "Final Array: ";
    for (int x : res) cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; " ";
    cout &amp;lt;&amp;lt; endl;
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

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

&lt;h2&gt;
  
  
  Open Source Contribution
&lt;/h2&gt;

&lt;p&gt;I am building an open-source repository of DSA solutions in C++, organized algorithm-wise for easier learning.&lt;br&gt;
Feel free to explore, star ⭐, and contribute to my repository:&lt;br&gt;
&lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;👉 DSA in C++ – GitHub Repository&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
      <category>dsa</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Subarray Sum Divisible By K.</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Mon, 21 Jul 2025 09:05:38 +0000</pubDate>
      <link>https://dev.to/umair01/subarray-sum-divisible-by-k-40ai</link>
      <guid>https://dev.to/umair01/subarray-sum-divisible-by-k-40ai</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;🧠 Intuition:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The key insight is that:&lt;/p&gt;

&lt;p&gt;If the difference between two prefix sums is divisible by k, then the subarray between those two indices has a sum divisible by k.&lt;/p&gt;

&lt;p&gt;This is where modular arithmetic helps:&lt;br&gt;
&lt;code&gt;If prefixSum[j] % k == prefixSum[i] % k, then prefixSum[j] - prefixSum[i] is divisible by k.&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;🔍 Approach (Prefix Sum + HashMap):&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Prefix Sum: Keep a running sum (prefixSum) while iterating through the array.&lt;/p&gt;

&lt;p&gt;Modulo Operation: For each prefix sum, compute mod = prefixSum % k.&lt;/p&gt;

&lt;p&gt;Normalization: Handle negative remainders by converting them into positive:&lt;code&gt;if (mod &amp;lt; 0) mod += k;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;HashMap: Use a map to store frequency of each mod value seen so far.&lt;/p&gt;

&lt;p&gt;If the same mod has occurred before, it means a subarray ending at current index has a sum divisible by k.&lt;/p&gt;

&lt;p&gt;Count: Increase count by the frequency of the current mod seen before.&lt;/p&gt;

&lt;p&gt;Initialization: Start with prefixMap[0] = 1 to handle cases where a subarray from the beginning is divisible by k.&lt;/p&gt;
&lt;h2&gt;
  
  
  📊 Time and Space Complexity:
&lt;/h2&gt;

&lt;p&gt;Metric Value Explanation&lt;br&gt;
🕒 Time Complexity O(n) Single pass through array&lt;br&gt;
🧠 Space Complexity O(k) Hash map stores at most k unique remainders&lt;/p&gt;
&lt;h2&gt;
  
  
  ✅ Example:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Input:&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;`nums = [4, 5, 0, -2, -3, 1], k = 5
Prefix sum steps:

Prefix sum = 4 → 4 % 5 = 4

Prefix sum = 9 → 9 % 5 = 4 → Found a match (same mod as before)

Prefix sum = 9 → 9 % 5 = 4 → Another match

Prefix sum = 7 → 7 % 5 = 2

Prefix sum = 4 → 4 % 5 = 4 → More matches!

Prefix sum = 5 → 5 % 5 = 0 → New match`
Answer: 7 valid subarrays.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int subarraysDivByK(vector&amp;lt;int&amp;gt;&amp;amp; nums, int k) {
      int cnt = 0, pf = 0;
      unordered_map&amp;lt;int, int&amp;gt;mp;

      mp[0] = 1;

      for(int num : nums){
        pf += num;

        int mod = pf % k;

        if(mod &amp;lt; 0){
            mod += k;
        }

        if(mp.find(mod) != mp.end()){
            cnt += mp[mod];
            mp[mod]++;
        }else{
            mp[mod] = 1;
        }
      }
      return cnt;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📁 Organized DSA Repository&lt;br&gt;
I've started a DSA-in-C++ GitHub repository where all problems are sorted algorithm-wise into folders like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Arrays → Prefix Sum, Sliding Window, Two Pointers&lt;/li&gt;
&lt;li&gt;Linked List&lt;/li&gt;
&lt;li&gt;HashMap &amp;amp; Sets&lt;/li&gt;
&lt;li&gt;Stack &amp;amp; Queues&lt;/li&gt;
&lt;li&gt;Trees, Graphs, and more...&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;📌 This solution belongs to the folder:&lt;br&gt;
&lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;https://github.com/UmairDevloper/DSA-in-C-.git&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>cpp</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Max Consecutive Ones III</title>
      <dc:creator>M Umair Ullah</dc:creator>
      <pubDate>Mon, 21 Jul 2025 09:01:12 +0000</pubDate>
      <link>https://dev.to/umair01/max-consecutive-ones-iii-2nb0</link>
      <guid>https://dev.to/umair01/max-consecutive-ones-iii-2nb0</guid>
      <description>&lt;p&gt;&lt;strong&gt;🧠 Intuition:&lt;/strong&gt;&lt;br&gt;
We are given a binary array &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;(0s and 1s), &lt;br&gt;
and an integer k which represents the maximum number of 0s we can flip to 1s.&lt;br&gt;
We need to find the maximum number of consecutive 1s in the array if we are allowed to flip at most k 0s.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a sliding window problem. The idea is to maintain a window [left, right] such that at most k zeros are in the window. As we expand the window to the right, if the count of zeros exceeds k, we shrink it from the left until we have at most k zeros again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🔍 Approach (Using Sliding Window):&lt;/strong&gt;&lt;br&gt;
Initialize two pointers: left = 0 and right = 0, and a variable zeros = 0 to count the number of 0s in the current window.&lt;/p&gt;

&lt;p&gt;Start expanding the window by moving right:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;If nums[right] == 0, increment zeros.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;While zeros &amp;gt; k, shrink the window from the left:&lt;/p&gt;

&lt;p&gt;If nums[left] == 0, decrement zeros.&lt;/p&gt;

&lt;p&gt;Move left++.&lt;br&gt;
At each step, calculate the window length: right - left + 1 and track the maximum.&lt;/p&gt;

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

&lt;p&gt;Space complexity:&lt;br&gt;
O(1)&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;class Solution {
public:
    int longestOnes(vector&amp;lt;int&amp;gt;&amp;amp; nums, int k) {
       int n = nums.size();
       int left = 0;

       for(int right = 0;right&amp;lt;n;right++){
        if(nums[right] == 0){
            k--;
        }

        if(k &amp;lt; 0){
            if(nums[left] == 0){
                k++;
            }
            left++;
        }
       }

       return n - left;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📁 Organized DSA Repository&lt;br&gt;
I've started a DSA-in-C++ GitHub repository where all problems are sorted algorithm-wise into folders like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Arrays → Prefix Sum, Sliding Window, Two Pointers&lt;/li&gt;
&lt;li&gt;Linked List&lt;/li&gt;
&lt;li&gt;HashMap &amp;amp; Sets&lt;/li&gt;
&lt;li&gt;Stack &amp;amp; Queues&lt;/li&gt;
&lt;li&gt;Trees, Graphs, and more...&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;📌 This solution belongs to the folder: &lt;a href="https://github.com/UmairDevloper/DSA-in-C-.git" rel="noopener noreferrer"&gt;https://github.com/UmairDevloper/DSA-in-C-.git&lt;/a&gt;&lt;/p&gt;

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