<?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: StriKing_sHadOws</title>
    <description>The latest articles on DEV Community by StriKing_sHadOws (@striking_shadows_b8f1b3de).</description>
    <link>https://dev.to/striking_shadows_b8f1b3de</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%2F3936982%2F4ea8a0d3-59eb-4a79-aad3-1168c04e9219.png</url>
      <title>DEV Community: StriKing_sHadOws</title>
      <link>https://dev.to/striking_shadows_b8f1b3de</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/striking_shadows_b8f1b3de"/>
    <language>en</language>
    <item>
      <title>3 and 4 Sum optimized Approach</title>
      <dc:creator>StriKing_sHadOws</dc:creator>
      <pubDate>Mon, 18 May 2026 17:25:01 +0000</pubDate>
      <link>https://dev.to/striking_shadows_b8f1b3de/3-and-4-sum-optimized-approach-52ko</link>
      <guid>https://dev.to/striking_shadows_b8f1b3de/3-and-4-sum-optimized-approach-52ko</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;u&gt;3 Sum problem(2 pointer approach)&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Here,I discussed classic problem of both 3 Sum and 4 Sum problem. &lt;br&gt;
3 Sum Problem&lt;br&gt;
Problem Statement &lt;/p&gt;

&lt;p&gt;Find all unique triplets in an array whose sum equals 0.&lt;br&gt;
ex-[-1, 0, 1, 2, -1, -4] o/p-[-1, -1, 2],&lt;br&gt;
[-1, 0, 1]&lt;/p&gt;

&lt;p&gt;Approach Using Two Pointers&lt;br&gt;
Step 1 — Sort the Array&lt;/p&gt;

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

&lt;p&gt;Use two pointers efficiently&lt;br&gt;
Handle duplicates easily&lt;/p&gt;

&lt;p&gt;Step 2 — Fix One Element&lt;/p&gt;

&lt;p&gt;Choose one element at a time.&lt;/p&gt;

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

&lt;p&gt;Fix -1&lt;/p&gt;

&lt;p&gt;Now we need two more numbers whose sum becomes 1&lt;/p&gt;

&lt;p&gt;Step 3 — Use Two Pointers&lt;/p&gt;

&lt;p&gt;Place:&lt;/p&gt;

&lt;p&gt;One pointer after the fixed element&lt;br&gt;
One pointer at the end&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;p&gt;If the sum is too small → move left pointer forward&lt;br&gt;
If the sum is too large → move right pointer backward&lt;br&gt;
If the required sum is found → store the triplet&lt;/p&gt;

&lt;p&gt;This reduces the complexity from O(n³) to O(n²).&lt;/p&gt;

&lt;p&gt;Important Observation&lt;/p&gt;

&lt;p&gt;Since the array is sorted:&lt;/p&gt;

&lt;p&gt;Moving left pointer increases the sum&lt;br&gt;
Moving right pointer decreases the sum&lt;/p&gt;

&lt;p&gt;This is the main reason why the two pointer technique works efficiently.&lt;/p&gt;

&lt;p&gt;Handling Duplicates&lt;/p&gt;

&lt;p&gt;Duplicate triplets should not appear in the final answer.&lt;/p&gt;

&lt;p&gt;So while traversing:&lt;/p&gt;

&lt;p&gt;Skip repeated fixed elements&lt;br&gt;
Skip repeated pointer values after finding a valid triplet&lt;/p&gt;

&lt;p&gt;This ensures only unique triplets are stored.&lt;/p&gt;

&lt;p&gt;Time Complexity of 3 Sum&lt;br&gt;
| Approach             | Complexity |&lt;br&gt;
| -------------------- | ---------- |&lt;br&gt;
| Brute Force          | O(n³)      |&lt;br&gt;
| Two Pointer Approach | O(n²)      |&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;4 Sum&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Problem Statement&lt;/p&gt;

&lt;p&gt;Find all unique quadruplets whose sum equals a given target.&lt;/p&gt;

&lt;p&gt;Example:nums = [1,0,-1,0,-2,2]&lt;br&gt;
target = 0&lt;br&gt;
o/p-[-2,-1,1,2]&lt;br&gt;
[-2,0,0,2]&lt;br&gt;
[-1,0,0,1]&lt;/p&gt;

&lt;p&gt;Approach Using Two Pointers&lt;/p&gt;

&lt;p&gt;The logic is very similar to the 3 Sum problem.&lt;/p&gt;

&lt;p&gt;Step 1 — Sort the Array&lt;/p&gt;

&lt;p&gt;Sorting again helps in:&lt;/p&gt;

&lt;p&gt;Efficient traversal&lt;br&gt;
Duplicate handling&lt;br&gt;
Applying two pointers&lt;br&gt;
Step 2 — Fix Two Elements&lt;/p&gt;

&lt;p&gt;In 4 Sum:&lt;/p&gt;

&lt;p&gt;First fix one element&lt;br&gt;
Then fix a second element&lt;/p&gt;

&lt;p&gt;Now we only need to find two remaining numbers.&lt;/p&gt;

&lt;p&gt;This becomes similar to the 2 Sum problem.&lt;/p&gt;

&lt;p&gt;Step 3 — Apply Two Pointers&lt;/p&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;p&gt;Left pointer&lt;br&gt;
Right pointer&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;p&gt;Increase left pointer if sum is smaller&lt;br&gt;
Decrease right pointer if sum is larger&lt;br&gt;
Store answer if target is achieved&lt;br&gt;
Why 4 Sum Becomes Efficient&lt;/p&gt;

&lt;p&gt;Brute force checks every quadruplet:O(n⁴)&lt;/p&gt;

&lt;p&gt;Using sorting + two pointers:O(n³)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/soumya526/3-sum-and-4-sum/tree/main/3%20and%204%20sum%20problem" rel="noopener noreferrer"&gt;Click it to see the codes of both&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;The 3 Sum and 4 Sum problems are powerful examples of:&lt;/p&gt;

&lt;p&gt;How sorting simplifies problems&lt;br&gt;
How two pointers reduce complexity&lt;br&gt;
How observation can optimize brute force solutions&lt;/p&gt;

&lt;p&gt;The two pointer technique is widely used in:&lt;/p&gt;

&lt;p&gt;Pair sum problems&lt;br&gt;
Sliding window problems&lt;br&gt;
Array optimization questions&lt;br&gt;
Interview-level DSA questions&lt;/p&gt;

&lt;p&gt;Mastering this technique makes many advanced problems easier.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;The biggest lesson from 3 Sum and 4 Sum is that:&lt;/p&gt;

&lt;p&gt;A sorted array combined with smart pointer movement can eliminate unnecessary computations efficiently.&lt;/p&gt;

&lt;p&gt;Instead of checking every possible combination:&lt;/p&gt;

&lt;p&gt;We intelligently move pointers&lt;br&gt;
Reduce time complexity&lt;br&gt;
Avoid duplicates naturally&lt;/p&gt;

&lt;p&gt;These problems are a must-learn for anyone preparing for coding interviews or competitive programming.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>dsa</category>
      <category>leetcode</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>3 Sum Problem</title>
      <dc:creator>StriKing_sHadOws</dc:creator>
      <pubDate>Mon, 18 May 2026 00:51:10 +0000</pubDate>
      <link>https://dev.to/striking_shadows_b8f1b3de/3-sum-problem-129m</link>
      <guid>https://dev.to/striking_shadows_b8f1b3de/3-sum-problem-129m</guid>
      <description>&lt;p&gt;**Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.&lt;/p&gt;

&lt;p&gt;Notice that the solution set must not contain duplicate triplets.&lt;/p&gt;

&lt;p&gt;The way to solve this problem is given in following repository link below:&lt;/p&gt;

&lt;p&gt;I have only discussed bruteforce and better approach**&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/soumya526/3-Sum" rel="noopener noreferrer"&gt;click it to preview code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
