From b4671c959adbb9080eae4355d19bf1d0c183131e Mon Sep 17 00:00:00 2001 From: subbu4061 Date: Mon, 12 Jan 2026 08:16:56 -0800 Subject: [PATCH] Adding all Two-Pointers-1 Solutions --- 3sum.java | 39 ++++++++++++++++++++++++++++++++++ container-with-most-water.java | 25 ++++++++++++++++++++++ sort-colors.java | 31 +++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 3sum.java create mode 100644 container-with-most-water.java create mode 100644 sort-colors.java diff --git a/3sum.java b/3sum.java new file mode 100644 index 00000000..41ab37e1 --- /dev/null +++ b/3sum.java @@ -0,0 +1,39 @@ +// Time Complexity :O(n^2) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Three line explanation of solution in plain english : We sort the array to make it easier to avoid duplicates and use the two-pointer technique. For each element, it sets two pointers (left and right) and moves them toward each other to find pairs that sum with the current element to zero. It skips duplicate elements to ensure each triplet is unique. All valid triplets are added to a list, which is returned at the end. + + +class Solution { + public List> threeSum(int[] nums) { + if(nums == null || nums.length<3) new ArrayList<>(); + Arrays.sort(nums); + List> output = new ArrayList<>(); + + + for(int i=0; i 0) break; + if(i>0 && nums[i] == nums[i-1]) { + continue; + } + int left = i+1; + int right = nums.length-1; + while(left 0) { + right--; + } else { + left++; + } + } + } + return output; + } +} diff --git a/container-with-most-water.java b/container-with-most-water.java new file mode 100644 index 00000000..6b4c22f9 --- /dev/null +++ b/container-with-most-water.java @@ -0,0 +1,25 @@ +// Time Complexity :O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Three line explanation of solution in plain english : We start with two pointers at the two ends of the array. +// At each step, we calculate the area formed by the two lines and update the maximum area. +// We move the pointer with the smaller height inward, since moving the taller one cannot increase the area. + +class Solution { + public int maxArea(int[] height) { + int pointer1 = 0; + int pointer2 = height.length -1; + int maxArea = 0; + while(pointer1 != pointer2) { + int curArea = Math.min(height[pointer1], height[pointer2]) * (pointer2 - pointer1); + maxArea = Math.max(maxArea, curArea ); + if(height[pointer1] < height[pointer2]) { + pointer1 +=1; + } else { + pointer2 -=1; + } + + } + return maxArea; + } +} diff --git a/sort-colors.java b/sort-colors.java new file mode 100644 index 00000000..93bdd638 --- /dev/null +++ b/sort-colors.java @@ -0,0 +1,31 @@ +// Time Complexity :O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Three line explanation of solution in plain english : I take three pointers. One on the left, one on the right and another starting from left and traverse through the array. While traversing if it finds 2, then it swaps with the right pointer and if it finds 0 it swaps with left pointer. It traverse until it meets right pointer. + + +class Solution { + public void sortColors(int[] nums) { + int left = 0; + int mid = 0; + int right = nums.length -1; + while(mid <= right) { + if(nums[mid] == 2) { + swap(nums, mid, right); + right--; + } else if (nums[mid] ==0) { + swap(nums, mid, left); + left++; + mid++; + } else { + mid++; + } + } + } + + private void swap(int[] nums, int pointer1, int pointer2) { + int temp = nums[pointer1]; + nums[pointer1] = nums[pointer2]; + nums[pointer2] = temp; + } +} \ No newline at end of file