From 349d3f6a05a8dfae961f55f4428f0505381fd434 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Thu, 8 Jan 2026 23:15:29 -0600 Subject: [PATCH 1/3] Add two solutions for threeSum problem Implemented two methods for the threeSum problem: one using hashing and another using sorting with two pointers. --- Leetcode_15.java | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Leetcode_15.java diff --git a/Leetcode_15.java b/Leetcode_15.java new file mode 100644 index 00000000..31a3fead --- /dev/null +++ b/Leetcode_15.java @@ -0,0 +1,103 @@ +//way1: +//Hold one value; Perform hashing on remaing values to get two elements such that the sum of holded value and another two elements from hashing =0. +//tc: o(n^2); sc: o(n) +class Solution { + public List> threeSum(int[] nums) { + Set> ans = new HashSet<>(); + List ansStr=new ArrayList<>(); + int n=nums.length; + for(int i=0;i(ans); + } + + private void helper(int[] nums,int i,int req,Set> ans,int val,List ansStr){ + // Map map=new HashMap<>(); + Set set=new HashSet<>(); + + for(int j=i;j curr=new ArrayList<>(); + int need=req-nums[j]; + + // StringBuilder sb=new StringBuilder(); + curr.add(val); + curr.add(nums[j]); + curr.add(req-nums[j]); + Collections.sort(curr); + + // for(int v:curr){ + // sb.append(v); + // sb.append(v); + // sb.append(v); + // } + // StringBuilder sb=new StringBuilder(); + + + // System.out.println(sb.toString()); + // System.out.println(curr); + + // if(!(ansStr.contains(sb.toString()))){ + // ans.add(curr); + // ansStr.add(sb.toString()); + // } + ans.add(curr); + }else{ + set.add(nums[j]); + } + } + } +} + +//way2: +//Sort arrays.. Hold one value and apply two pointer on remaining values to get desired sum +//tc: o(n^2); sc: o(1) +class Solution { + public List> threeSum(int[] nums) { + List> ans = new ArrayList<>(); + Arrays.sort(nums); + + int n=nums.length; + + for(int i=0;i curr=new ArrayList<>(); + //Array is sorted. Need not perform sorting again + curr.add(nums[i]); + curr.add(nums[l]); + curr.add(nums[h]); + ans.add(curr); + l++; + h--; + + while(l Date: Thu, 8 Jan 2026 23:16:10 -0600 Subject: [PATCH 2/3] Add color sorting algorithm using three pointers Implement Dutch National Flag algorithm to sort colors. --- Leetcode_75.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Leetcode_75.java diff --git a/Leetcode_75.java b/Leetcode_75.java new file mode 100644 index 00000000..75de0a0d --- /dev/null +++ b/Leetcode_75.java @@ -0,0 +1,36 @@ +//Three pointers +//low - to store 0's +//high - to store 2's +//mid travel +//if nums[mid]==2 --> swap with high pointer +//if nums[mid]==0 --> swap with low pointer +//if nums[mid]==1, don't swap. Just increment pointer. If later 0 comes, then swap will happen. + +//tc: o(n), sc: o(1) +class Solution { + public void sortColors(int[] nums) { + int n=nums.length; + + int low=0,high=n-1,mid=0; + + while(mid<=high){ + if(nums[mid]==2){ + int temp=nums[high]; + nums[high]=nums[mid]; + nums[mid]=temp; + high--; + }else if(nums[mid]==0){ + int temp=nums[low]; + nums[low]=nums[mid]; + nums[mid]=temp; + low++; + mid++; + }else if(nums[mid]==1){ + mid++; + } + + + } + + } +} From 5216d5702b3f90b22e4730f321b3cc50a33c3cae Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Thu, 8 Jan 2026 23:17:19 -0600 Subject: [PATCH 3/3] Add maxArea method for container with most water Implement maxArea method to calculate maximum water area. --- Leetcode_11.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Leetcode_11.java diff --git a/Leetcode_11.java b/Leetcode_11.java new file mode 100644 index 00000000..ed0f4992 --- /dev/null +++ b/Leetcode_11.java @@ -0,0 +1,26 @@ +//Highest area-- high height and high width +//high width can be obtained with distance between low and high pointer. +//so, start low from one side and high from another side to obtain max width +//Capability to store high water at that place will comes from low container. So, low container servees as height +//Area=width * height +//move container with low heigh +//o(m+n) ; sc: o(1) +class Solution { + public int maxArea(int[] height) { + int area=0; + int low=0,high=height.length-1; + + while(low