From c2fe62dcfb5a607ed5ecd53b9364d44d5237368b Mon Sep 17 00:00:00 2001 From: Hriday-A Date: Sat, 25 Apr 2026 00:05:02 -0500 Subject: [PATCH] Completed Two-Pointers-1 Assignment --- 3sum.java | 30 ++++++++++++++++++++++++++++++ container_most_water.java | 24 ++++++++++++++++++++++++ sort_colors.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 3sum.java create mode 100644 container_most_water.java create mode 100644 sort_colors.java diff --git a/3sum.java b/3sum.java new file mode 100644 index 00000000..e159ec7d --- /dev/null +++ b/3sum.java @@ -0,0 +1,30 @@ +// Time Complexity : O(n^2) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Explaination : We use a two-pointer approach after sorting the array to find all unique triplets that sum to zero. + +class Solution { + public List> threeSum(int[] nums) { + Arrays.sort(nums); + int n = nums.length; + List> result = new ArrayList<>(); + for(int i=0;i0) break; + int low=i+1, high= n-1; + while(low0) high--; + else low++; + } + } + return result; + } +} \ No newline at end of file diff --git a/container_most_water.java b/container_most_water.java new file mode 100644 index 00000000..b46aee0e --- /dev/null +++ b/container_most_water.java @@ -0,0 +1,24 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Explaination : We use two pointers to calculate the area and +// move the pointer with smaller height to find the maximum area. +class Solution { + public int maxArea(int[] height) { + int n = height.length; + int l =0; + int r = n-1; + int max=0; + while(l