From 8cde84651b2fa8542f174807362b8281ee7d5a5b Mon Sep 17 00:00:00 2001 From: Amaan-29 Date: Fri, 9 Jan 2026 17:01:07 -0500 Subject: [PATCH 1/3] Two-pointer HW --- ContainerWithMostWater.java | 2 ++ SortColors.java | 2 ++ ThreeSum.java | 2 ++ 3 files changed, 6 insertions(+) create mode 100644 ContainerWithMostWater.java create mode 100644 SortColors.java create mode 100644 ThreeSum.java diff --git a/ContainerWithMostWater.java b/ContainerWithMostWater.java new file mode 100644 index 00000000..ee681d2f --- /dev/null +++ b/ContainerWithMostWater.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class ContainerWithMostWater { +} diff --git a/SortColors.java b/SortColors.java new file mode 100644 index 00000000..0cadbfd2 --- /dev/null +++ b/SortColors.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class SortColors { +} diff --git a/ThreeSum.java b/ThreeSum.java new file mode 100644 index 00000000..946c1c8b --- /dev/null +++ b/ThreeSum.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class ThreeSum { +} From 8c97449859784b3d3bc0980d8a32b27603a5ed22 Mon Sep 17 00:00:00 2001 From: Amaan-29 Date: Fri, 9 Jan 2026 17:02:35 -0500 Subject: [PATCH 2/3] Two-pointer HW --- .idea/.gitignore | 3 + .idea/Two-Pointers-1.iml | 11 ++++ .idea/codeStyles/Project.xml | 7 +++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ ContainerWithMostWater.java | 41 ++++++++++++- SortColors.java | 38 +++++++++++- ThreeSum.java | 57 +++++++++++++++++- .../Two-Pointers-1/.idea/.gitignore | 3 + .../Two-Pointers-1/.idea/Two-Pointers-1.iml | 11 ++++ .../.idea/codeStyles/Project.xml | 7 +++ .../.idea/codeStyles/codeStyleConfig.xml | 5 ++ out/production/Two-Pointers-1/.idea/misc.xml | 6 ++ .../Two-Pointers-1/.idea/modules.xml | 8 +++ out/production/Two-Pointers-1/.idea/vcs.xml | 6 ++ .../ContainerWithMostWater.class | Bin 0 -> 1002 bytes out/production/Two-Pointers-1/README.md | 8 +++ .../Two-Pointers-1/SortColors.class | Bin 0 -> 1215 bytes out/production/Two-Pointers-1/ThreeSum.class | Bin 0 -> 1588 bytes 21 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Two-Pointers-1.iml create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 out/production/Two-Pointers-1/.idea/.gitignore create mode 100644 out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml create mode 100644 out/production/Two-Pointers-1/.idea/codeStyles/Project.xml create mode 100644 out/production/Two-Pointers-1/.idea/codeStyles/codeStyleConfig.xml create mode 100644 out/production/Two-Pointers-1/.idea/misc.xml create mode 100644 out/production/Two-Pointers-1/.idea/modules.xml create mode 100644 out/production/Two-Pointers-1/.idea/vcs.xml create mode 100644 out/production/Two-Pointers-1/ContainerWithMostWater.class create mode 100644 out/production/Two-Pointers-1/README.md create mode 100644 out/production/Two-Pointers-1/SortColors.class create mode 100644 out/production/Two-Pointers-1/ThreeSum.class diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Two-Pointers-1.iml b/.idea/Two-Pointers-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Two-Pointers-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..700c1aac --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ContainerWithMostWater.java b/ContainerWithMostWater.java index ee681d2f..8b6066eb 100644 --- a/ContainerWithMostWater.java +++ b/ContainerWithMostWater.java @@ -1,2 +1,41 @@ -package PACKAGE_NAME;public class ContainerWithMostWater { +public class ContainerWithMostWater { + public static void main(String args[]){ + //int [] arr = {1,8,6,2,5,4,8,3,7}; + int [] arr ={2,5,3,8,10,6,12,10}; + System.out.println(maxArea(arr)); + } + + public static int maxArea(int[] height) { + /* + //Brute-force approach + int n = height.length; + int maxArea = 0; + for(int i =0; i total time complexity O(n^2) + int currArea = Math.min(height[i], height[j]) * (j-i); + maxArea = Math.max(maxArea,currArea); + } + } + */ + + //two-pointer approach + int n = height.length; + int maxArea = 0; + int left = 0; + int right = n-1; + while(left> threeSum(int[] nums) { + int n = nums.length; + //Condition to check if length of array less than 3 or if its empty + if (nums == null || n < 3) return new ArrayList<>(); + + //Step 1: Sort the elements in array + Arrays.sort(nums); + Set> result = new HashSet<>(); + //Step 2: Using Two-pointer approach + //Fix position of first element i.e. nums[i], traverse until < n-2 as we need triplets in end + for (int i = 0; i < n - 2; i++) { //0(n) + int l = i + 1; + int r = n - 1; + while (l < r) { //0(n) + int sum = nums[i] + nums[l] + nums[r]; + if (sum == 0) { + //Add the set and move to find other triplets, increment left and decrement right + result.add(Arrays.asList(nums[i], nums[l], nums[r])); + l++; + r--; + } else if (sum < 0) { + l++; + } else { + r--; + } + } + } + return new ArrayList<>(result); + } + /* + //Brute-force approach + for(int i=0; i(result); + } + */ + + public static void main(String args[]){ + int [] arr1 ={-1,0,1,2,-1,-4}; + System.out.println(threeSum(arr1)); + } } + +//Time complexity : 0(n^2) +//Space complexity : O(n) \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/.gitignore b/out/production/Two-Pointers-1/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml b/out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/codeStyles/Project.xml b/out/production/Two-Pointers-1/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/codeStyles/codeStyleConfig.xml b/out/production/Two-Pointers-1/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/misc.xml b/out/production/Two-Pointers-1/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/modules.xml b/out/production/Two-Pointers-1/.idea/modules.xml new file mode 100644 index 00000000..700c1aac --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/vcs.xml b/out/production/Two-Pointers-1/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/ContainerWithMostWater.class b/out/production/Two-Pointers-1/ContainerWithMostWater.class new file mode 100644 index 0000000000000000000000000000000000000000..11b1303a6986d7261dbd6d6b986adbda0b599805 GIT binary patch literal 1002 zcmZuw%T5zv7(IXI){a9N!D68;B6w*_X$7$)mg0rPq)D+RH6;zJp^WWdFJwA3y5~vQ zuyLacCC2aoZhQ@&!5HyOOF~RK$^7$O&v(9)e}4S^b`GG34IKeU8e{_sQ~~qQdS~U@ zR=1g}>>t_vX-U z=h{}Mpd*Y)4W@zXm=c)T?sYxO>DunD)LqJLHl~N@gHxSV< zW8fyD0@Gu#*>vPjYIgN%VhU*(Al@$*VcZUk*RX8VYO?P>-hd8}u`KDBq zwqo)@UQR^h6Rf5a*>ptCo?$j3Ck|S9B_2`EaI1J3KSAhN5~om)<%`pcpYe}Gk%p_U zabbVD#Ji~=3JtT2%yDLn`8e4mu#R~=#sdGhMLcEH=etEFORQT#mVM+zZ8JmECwRb^ vuX>T(v)oHV0jsnmBImJ2OU6UiMj>A?q-ab>$M7roDE?<#MWb%Ab7sz*%kP|-`TYI;5r7Ob79ucANE-@KfyB1+ z%t@D>>Sj8>zFjH?0?|dc>IO>!#>nWZg&5){EE_Gb1*{d%50<^M=hp;e?YXlfV2-S1 zv)P8Jq3zpkB+xzNbfMcs(nb$@|H!_wTMJ4RCiUuMNajK=*GoV0-DM^}sEsZ~4BnOQcxf(blbQ3iRlNjWA((4LxI=#gK_%8|QFd zpz9xbSkjKpTCNggbgWx4c(^()`q+@drp?uJ0?O5hZqK5%xza3;oR>+;{G zJ)6cekQK*e1lIf?T*u#Js;+-*)7kO;DRR_`;Q`nEV(E^nYiU2hozQ*4Fi$tadjv=j z5>bv9xJpwBN;P%}@rIKKE^^e{dVYzc-7vsqjBv%nLkCCg{Efk)l=o9d8DnWcZp=ty zMkQ5JzQ$}SsV3AbbdGV7lm{^P*jy%cRbQUen+GBrq%PYonx z{-l|mG||Bndg<852KI-D(+$U8h#gHt24h(oQO~Z0h>8iPL&P|)W2Tw>Mc5!9o3K7w z1;fY}B!8vyg{aIIO#U&h6Hz_rRU*c%L|o-_9}p`XqV>Ue>HuwT(V2ROz5|>&f!4TS urzdd+b9{MAyh8W*@^o*9*fS=@4a`zbvZT4Nw{t9Qp0^}Iyanz=u=oSjjnC=; literal 0 HcmV?d00001 diff --git a/out/production/Two-Pointers-1/ThreeSum.class b/out/production/Two-Pointers-1/ThreeSum.class new file mode 100644 index 0000000000000000000000000000000000000000..121cc58320259b17cab201e368fc91baf575a69d GIT binary patch literal 1588 zcma)6O>Y}j6g|)LWltuJlhmdmIQ@Wv?WQ)6HU%64ZJ>?8i3_fROju+R56%>SMB{N$ zBvw@`Fx-mVAPz52X^%Njc98Omq=}wRJ-W^l>CWec;o(ktb0?^Je6#;P^`gW?M7xX`RP0wV>t;Vif7 z>yCHJUa!$IR%mY6wH4cQ<-TiF{Y{s}Hw(QybEMaj>54p`8Xu33OTUeFoygFtv@^c$ zIjwe$I*beuVF~CUemv6lRxjG@ap>H@R@Sz=AMC2!JpWncl5cN(vSe>{(~gwfYQy&1 z9(m=SAm;z~tk!LpY~sgykF|6gRrUgD+g_El#|K-pdmJ2kEV5Z@w!IDKx+`TK?kSm( zoyjJ>$ltO2Q@~#rbiQBVF3YdrS4;0h{KQ2FuktOACG<7EtquS=%yAcy4Mh|$i!X>X zXxmLcoJ;?K@T9(rp$9O^`-t32AHaNoDA!|4%6wu{tL&ncP}gSFNwqSo#nhO#i;vQY zOgg4!*3vO8vo@>8^zU$GTK^s6Wrarvh?UB!1OzKhqU7oT6Tcu4!UsF5*wK#mFdEap zM})gQ=+bcX72*)f z!hD3#UkE*7fOiNB;a!%efF(MRwRM<+ta&P`|7MnzP_e8k#j>Ur%eq!98{CJZhsDCr tc>clxrtjnBe*I4bna*&=T|yF<1D5fQGpbUg(<$n9lRRYmycYz2{~zh!UTFXT literal 0 HcmV?d00001 From 1039c7e1a348435e8bd2f8e36b91408c9b6067ff Mon Sep 17 00:00:00 2001 From: Amaan-29 Date: Fri, 9 Jan 2026 20:56:13 -0500 Subject: [PATCH 3/3] Two-pointer-1 Homework --- ContainerWithMostWater.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ContainerWithMostWater.java b/ContainerWithMostWater.java index 8b6066eb..5f43b648 100644 --- a/ContainerWithMostWater.java +++ b/ContainerWithMostWater.java @@ -23,7 +23,7 @@ public static int maxArea(int[] height) { int maxArea = 0; int left = 0; int right = n-1; - while(left