<?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: Ashu Sharma</title>
    <description>The latest articles on DEV Community by Ashu Sharma (@ashusharmacs).</description>
    <link>https://dev.to/ashusharmacs</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%2F490010%2Fd86ba4ef-23b9-41a2-828d-23912b04dee1.jpg</url>
      <title>DEV Community: Ashu Sharma</title>
      <link>https://dev.to/ashusharmacs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashusharmacs"/>
    <language>en</language>
    <item>
      <title>3Sum Closest</title>
      <dc:creator>Ashu Sharma</dc:creator>
      <pubDate>Sat, 21 Jun 2025 05:11:41 +0000</pubDate>
      <link>https://dev.to/ashusharmacs/3sum-closest-518m</link>
      <guid>https://dev.to/ashusharmacs/3sum-closest-518m</guid>
      <description>&lt;h2&gt;
  
  
  So I Heard You Like ...Sums? My Take on 3Sum Closes
&lt;/h2&gt;

&lt;p&gt;Alright, it's becoming a habit to share my first reaction to these problems, so here we go. When I saw "3Sum Closest," my brain pretty much went:&lt;/p&gt;

&lt;p&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%2Fd0eg7wef2fg8rkpn1tqs.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%2Fd0eg7wef2fg8rkpn1tqs.png" alt="same spiderman meme" width="800" height="735"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I mean, I'd already conquered 2Sum, 3Sum, and even battled through 4Sum. Don't get me wrong, I love a good trilogy (and its questionable fourth installment), but this felt like a sequel I didn't ask for.&lt;/p&gt;

&lt;p&gt;But this one is a little different. It’s got a fun twist! We don't need to find a triplet that equals a target. Instead, we need to find the one whose sum is the closest.&lt;/p&gt;

&lt;p&gt;There could be many triplets, and we need to figure out which one is the "closest." So, what does "closer" even mean here? Well, think of it like a number line. You just subtract the triplet's sum from the target and take the absolute value. The smaller that result, the cozier you are to the target. That's our mission.&lt;/p&gt;

&lt;p&gt;The game plan is actually pretty similar to the original 3Sum. Ohh! and we  need to sort our vector/array before we do anything. Now you start with a for loop that runs through each element, let's call its iterator i.&lt;/p&gt;

&lt;p&gt;From there, we get to use the classic two-pointer strategy. We'll plant a left pointer at i + 1 and a right pointer at the very end of the array. Now we have our triplet!&lt;/p&gt;

&lt;p&gt;With our three numbers, we calculate the sum. Then, as I mentioned, we find the current_difference between this sum and our target. This is where we need to keep score. We'll have a couple of variables, min_difference and closest_sum, to remember the best we've seen so far.&lt;/p&gt;

&lt;p&gt;Every time we calculate a new sum, we check: is this new guy even closer than our current champion?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (abs(target - sum) &amp;lt; min_difference) {
    min_difference = abs(target - sum);
    closest_sum = sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple enough, right? Now we just need to move our pointers in that slick, efficient way we all learned from the 2Sum problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (sum &amp;lt; target) {
    left++;
} else if (sum &amp;gt; target) {
    right--;
} else {
    return sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you might be looking at that else block and thinking, "Hold on, why are you returning the sum right away?"&lt;/p&gt;

&lt;p&gt;Think about it for a second. If our sum actually equals the target, can we possibly get any closer? Nope! That's a bullseye. It's the undisputed, all-time champion of "closest." So, we can just return it and call it a day.&lt;/p&gt;

&lt;p&gt;But if we get through the whole loop and never hit that perfect match, no sweat! Our closest_sum variable has been faithfully remembering the next best thing the whole time. We just return that at the very end.&lt;/p&gt;

&lt;p&gt;And that's it! That's the 3Sum Closest problem for you.&lt;/p&gt;

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

</description>
      <category>dsa</category>
      <category>cpp</category>
      <category>leetcode</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Dutch National Flag Problem</title>
      <dc:creator>Ashu Sharma</dc:creator>
      <pubDate>Sat, 14 Jun 2025 03:22:44 +0000</pubDate>
      <link>https://dev.to/ashusharmacs/dutch-national-flag-problem-1o30</link>
      <guid>https://dev.to/ashusharmacs/dutch-national-flag-problem-1o30</guid>
      <description>&lt;p&gt;So this time I was given the below problem:&lt;/p&gt;

&lt;p&gt;Given an array 'nums' with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.&lt;/p&gt;

&lt;p&gt;We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.&lt;/p&gt;

&lt;p&gt;You must solve this problem without using the library's sort function.&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;My brain immediately went:&lt;/p&gt;

&lt;p&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%2Fmt4da5s84hm57z9o6vem.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%2Fmt4da5s84hm57z9o6vem.png" alt="I can do this all day" width="500" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I had a gut feeling this was about rearranging and grouping elements. My intuition screamed "pointers!" Not just two, but three. Also I get to know  that this classic problem is often called the Dutch National Flag problem.&lt;/p&gt;

&lt;p&gt;The Game Plan: Three Pointers for Three Colors&lt;br&gt;
The goal is to partition the array into three sections: all the 0s at the beginning, all the 1s in the middle, and all the 2s at the end.&lt;/p&gt;

&lt;p&gt;To do this in a single pass, we'll use three pointers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;left: This pointer marks the boundary for our "red" (0) section. Everything to the left of left is a confirmed 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;middle: This is our main iterator. It scans the array, and its job is to figure out where the element nums[middle] belongs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;right: This pointer marks the boundary for our "blue" (2) section. Everything to the right of right is a confirmed 2.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our loop will run as long as middle &amp;lt;= right, shrinking the "unknown" section between middle and right until it disappears.&lt;/p&gt;

&lt;p&gt;The Logic in Action&lt;br&gt;
We iterate through the array with our middle pointer and handle three simple cases:&lt;/p&gt;

&lt;p&gt;Case 1: nums[middle] is a 0 (Red) 🔴&lt;br&gt;
If nums[middle] is a 0, it belongs in the left section. So, we swap the element at middle with the element at left.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;swap(nums[left], nums[middle]);

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

&lt;/div&gt;



&lt;p&gt;Since we now have a confirmed 0 at the left position, we can advance our left pointer. We also advance the middle pointer because the element we just swapped into the middle position has already been processed (we'll dive into why in a moment).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;left++;
middle++;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Case 2: nums[middle] is a 1 (White) ⚪&lt;br&gt;
If nums[middle] is a 1, it's already in the right potential spot! The middle section is where 1s are supposed to live. We don't need to swap anything. We just move on to the next element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;middle++;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Case 3: nums[middle] is a 2 (Blue) 🔵&lt;br&gt;
If nums[middle] is a 2, it belongs at the end of the array, in the right section. So, we swap it with the element at the right pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;swap(nums[middle], nums[right]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the swap, we know the element now at right is a 2, so we can shrink our right boundary inward.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;But notice we don't advance middle here! Why? The element we just received from the right position is a complete unknown. It could be a 0, 1, or even another 2. We need to re-evaluate the new element at nums[middle] in the next loop iteration.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "Aha!" Moment: Why Pointer Moves Differ
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This was the key insight for me&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;When we swap nums[middle] == 0 with nums[left], we know the element coming to middle was already in the processed zone (left of middle). Because of our algorithm's structure, any element between left and middle can only be a 1. So, after the swap, nums[middle] is guaranteed to be a 1 (or a 0 if left and middle started at the same spot). In either case, the element at middle is handled, and we can confidently move middle forward.&lt;/p&gt;

&lt;p&gt;When we swap nums[middle] == 2 with nums[right], the element coming to middle is from the unprocessed, unknown part of the array. We must re-check it to see if it's a 0, 1, or 2 before moving middle.&lt;/p&gt;

&lt;p&gt;This simple but powerful logic allows us to sort the array in one pass with constant extra space.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>sortcolor</category>
      <category>dsa</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Index Negation Technique</title>
      <dc:creator>Ashu Sharma</dc:creator>
      <pubDate>Sat, 07 Jun 2025 03:04:06 +0000</pubDate>
      <link>https://dev.to/ashusharmacs/index-negation-technique-19ep</link>
      <guid>https://dev.to/ashusharmacs/index-negation-technique-19ep</guid>
      <description>&lt;p&gt;So I was asked a question &lt;em&gt;find the missing number(s) in an array where the elements are in the range of [1,n] and it(array) contains n integers&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [4,3,2,7,8,2,3,1] (n=8, range=[1,8]) 
Output: {5,6}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My first Reaction &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%2F0235u3xyckbb45rdnsll.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%2F0235u3xyckbb45rdnsll.png" alt="yelling yay" width="430" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My first instinct was to go with a straightforward brute-force solution. But reality hit hard. If I go Brute Force, I will have to pick each number from 1 to n and then search for that number in my input array. That means if my array is too large, it's going to take a lot of operations (O(n^2) time complexity, to be exact). Nobody likes that.&lt;/p&gt;

&lt;p&gt;So, I discovered this clever approach called the Index Negation Technique.&lt;/p&gt;

&lt;h2&gt;
  
  
  THE INDEX NEGATION TECHNIQUE
&lt;/h2&gt;

&lt;p&gt;But before I explain this technique, I want to remind all of you of a very basic concept related to the problem's constraints:&lt;/p&gt;

&lt;p&gt;For an array where elements are in the range [1, n], if it were sorted, every element x would be placed at index x-1. For example, the number 1 would be at index 0, the number 2 at index 1, and so on.&lt;/p&gt;

&lt;p&gt;This mapping between a value and an index is the foundation of our trick.&lt;/p&gt;

&lt;p&gt;Now, here's how the technique works:&lt;/p&gt;

&lt;p&gt;You pick an element from the input array. Let's call this value &lt;em&gt;num&lt;/em&gt;. You then find the index where &lt;em&gt;num&lt;/em&gt; would have been placed if the array was sorted (which is index = abs(num) - 1).&lt;/p&gt;

&lt;p&gt;Once that index is found, you negate the value at that specific index to mark it as "seen". You do this for every element in the array.&lt;/p&gt;

&lt;p&gt;By doing this, you are using the array itself to keep track of which numbers have been found.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, why do we need &lt;em&gt;abs()&lt;/em&gt;?
&lt;/h2&gt;

&lt;p&gt;That's a great question, and it's the key to making this technique robust. The algorithm would break without using the absolute value (abs()) for two main reasons. Let's use our example: [4,3,2,7,8,2,3,1].&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To handle values at indices that have already been negated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Imagine our loop is processing the array. We start with the 4, then the 3. When we process the 3 (at index 1), we go to index 2 and negate the value there.&lt;/p&gt;

&lt;p&gt;Initial array: [4, 3, 2, 7, ...]&lt;br&gt;
After processing 3: [4, 3, -2, 7, ...]&lt;/p&gt;

&lt;p&gt;A moment later, our loop arrives at index 2 to process the element there. The value it reads is no longer 2, but -2.&lt;/p&gt;

&lt;p&gt;Without abs(): We would try to calculate the target index for -2, which would be -2 - 1 = -3. This is an invalid index and would crash the program.&lt;br&gt;
With abs(): We take abs(-2), which gives us 2. We then calculate the target index as 2 - 1 = 1. This correctly tells us that the number 2 corresponds to index 1, allowing the algorithm to proceed correctly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To correctly process duplicate numbers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our example has two 2s and two 3s. Let's see what happens with the two 2s.&lt;/p&gt;

&lt;p&gt;When the algorithm first encounters a 2, it goes to index 1 (since 2-1=1) and negates the value there.&lt;/p&gt;

&lt;p&gt;Later, when the algorithm encounters the second 2, it needs to do the same thing. It takes abs(2), finds index 1, and sees the value there is already negative. This confirms that a 2 has already been seen, so it simply moves on.&lt;/p&gt;

&lt;p&gt;In short, abs() ensures we always use a number's magnitude to find its correct home index. It makes the algorithm work regardless of the order of the elements or whether the values we are reading have already been flipped to negative by a previous step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the Final Answer
&lt;/h2&gt;

&lt;p&gt;Once you have iterated through the entire input array, you make one final pass. You can just see which values in the array are still positive. A positive value at index i implies that the number i+1 was never present in the original input. These are your missing values!&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>cpp</category>
      <category>interview</category>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
