Back to Browse

LeetCode_15: 3Sum | Sorting + Two Pointer Approach

May 16, 2026
11:45

In this video, I solve LeetCode Problem 15 - 3Sum using the Two Pointer approach in JavaScript. 🔍 What you'll learn: -Why the Brute Force O(n³) solution is too slow -How sorting helps optimize the problem -Using Two Pointers to reduce complexity -How to avoid duplicate triplets -Why we stop when nums[i] becomes positive -Understanding fixed pointer + left/right pointer logic 💡 At first, I tried solving the problem using three nested loops. The logic worked, but later I understood that the time complexity becomes O(n³), which is inefficient for large inputs. 💡 Then I learned the optimized Two Pointer approach. After sorting the array, we fix one number and use two pointers to search for the remaining two numbers whose sum becomes 0. 💡 We move the pointers depending on the current sum: -If sum is greater than 0 → move the right pointer -If sum is smaller than 0 → move the left pointer -If sum becomes 0 → store the triplet and skip duplicates ⏱ Time Complexity: O(n²) 📦 Space Complexity: O(1) extra space 📚 Problem Link: https://leetcode.com/problems/3sum/ 🚀 One of the most important interview problems for understanding sorting, Two Pointers, and optimization techniques. #leetcode #dsa #twopointers #javascript #arrays #coding #algorithms

Download

0 formats

No download links available.

LeetCode_15: 3Sum | Sorting + Two Pointer Approach | NatokHD