From 9413e00f6e6f759013c0867dfcdb0a414a9fdbdc Mon Sep 17 00:00:00 2001 From: Amaan-29 Date: Sun, 11 Jan 2026 01:24:31 -0500 Subject: [PATCH 1/2] Hashing-2 HW LongestPalindrome and SubarraySumEqualsK --- .idea/.gitignore | 3 ++ .idea/Hashing-2.iml | 11 +++++++ .idea/codeStyles/Project.xml | 7 +++++ .idea/codeStyles/codeStyleConfig.xml | 5 ++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ LongestPalindrome.java | 28 ++++++++++++++++++ SubarraySum.java | 26 ++++++++++++++++ out/production/Hashing-2/.idea/.gitignore | 3 ++ out/production/Hashing-2/.idea/Hashing-2.iml | 11 +++++++ .../Hashing-2/.idea/codeStyles/Project.xml | 7 +++++ .../.idea/codeStyles/codeStyleConfig.xml | 5 ++++ out/production/Hashing-2/.idea/misc.xml | 6 ++++ out/production/Hashing-2/.idea/modules.xml | 8 +++++ out/production/Hashing-2/.idea/vcs.xml | 6 ++++ .../Hashing-2/LongestPalindrome.class | Bin 0 -> 1318 bytes out/production/Hashing-2/README.md | 12 ++++++++ 18 files changed, 158 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Hashing-2.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 LongestPalindrome.java create mode 100644 SubarraySum.java create mode 100644 out/production/Hashing-2/.idea/.gitignore create mode 100644 out/production/Hashing-2/.idea/Hashing-2.iml create mode 100644 out/production/Hashing-2/.idea/codeStyles/Project.xml create mode 100644 out/production/Hashing-2/.idea/codeStyles/codeStyleConfig.xml create mode 100644 out/production/Hashing-2/.idea/misc.xml create mode 100644 out/production/Hashing-2/.idea/modules.xml create mode 100644 out/production/Hashing-2/.idea/vcs.xml create mode 100644 out/production/Hashing-2/LongestPalindrome.class create mode 100644 out/production/Hashing-2/README.md 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/Hashing-2.iml b/.idea/Hashing-2.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Hashing-2.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..ac128052 --- /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/LongestPalindrome.java b/LongestPalindrome.java new file mode 100644 index 00000000..bc0bac27 --- /dev/null +++ b/LongestPalindrome.java @@ -0,0 +1,28 @@ +import java.util.HashSet; + +public class LongestPalindrome { + public static int longestPalindrome(String s) { + HashSet set = new HashSet<>(); + int count = 0; + for(int i = 0; i < s.length(); i++){ + char ch = s.charAt(i); + //Check in hashset if elements exists, if pair matches then remove from set else add it + if(set.contains(ch)){ + set.remove(ch); + count +=2; + }else{ + set.add(ch); + } + } + //If hashset is not empty then add 1 of the element from set + if(!set.isEmpty()){ + count++; + } + return count; + } + public static void main(String[] args){ + String s = "abccccdd"; + System.out.println(longestPalindrome(s)); + } + +} diff --git a/SubarraySum.java b/SubarraySum.java new file mode 100644 index 00000000..19a34c97 --- /dev/null +++ b/SubarraySum.java @@ -0,0 +1,26 @@ +import java.util.HashMap; + +public class SubarraySum { + public int subarraySum(int[] nums, int k) { + //create a variable for running sum + int rSum = 0; + HashMap map = new HashMap<>(); + map.put(0,1); + int count = 0; + for(int i = 0; i < nums.length; i ++){ + rSum = rSum + nums[i]; + if(map.containsKey(rSum - k)){ + count = count + map.get(rSum - k); + } + if(!map.containsKey(rSum)){ + map.put(rSum,1); + }else { + map.put(rSum, map.get(rSum)+1); + } + } + return count; + } +} + +//Time complexity : O(n) +//Space complexity : O(n) diff --git a/out/production/Hashing-2/.idea/.gitignore b/out/production/Hashing-2/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/out/production/Hashing-2/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/Hashing-2/.idea/Hashing-2.iml b/out/production/Hashing-2/.idea/Hashing-2.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/out/production/Hashing-2/.idea/Hashing-2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/codeStyles/Project.xml b/out/production/Hashing-2/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/out/production/Hashing-2/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/codeStyles/codeStyleConfig.xml b/out/production/Hashing-2/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/out/production/Hashing-2/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/misc.xml b/out/production/Hashing-2/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/out/production/Hashing-2/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/modules.xml b/out/production/Hashing-2/.idea/modules.xml new file mode 100644 index 00000000..ac128052 --- /dev/null +++ b/out/production/Hashing-2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/.idea/vcs.xml b/out/production/Hashing-2/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/out/production/Hashing-2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-2/LongestPalindrome.class b/out/production/Hashing-2/LongestPalindrome.class new file mode 100644 index 0000000000000000000000000000000000000000..16fc9be1103b1cf24efe9d8b754147e6f7bca6e6 GIT binary patch literal 1318 zcmZuxTT|0O7(LsxOJt%IXJEom&#G-1+|8Q%F5`sj-< zzUV`BbjD}LpTs!m*@kK>wv+6B-+q^K&hF;N=9g~(uA!1b1e%Vhff(Wf125&OEI87w z73P;-T4o>+pR`>&m=e&kxkVibfx&iAGq9b)tZXb-tRMv)S_(U0=;$-hkDWcGDuHji zHI{NLw-zk7OUn^i}CJ zt@#%MBiT}}{GXVjLN#h&AI1a{rsoFIb{hhjY*%#1vY30Of)5xtsDk6ZRrgjcK1*@c3%&xQ1D`oB45H~ z9ajuoMX@7gZ$olfQSBW!lz)*!$*Z!>&a&-V51REQ%YQ7F9D0Vzo++J0>D$WQaz=w? zo3f_M+mNzj#N>915+9q-93O@bb#1^FRBF<&s7X3p>ONeo5;MJ~%hloD{%b284EgP8 zlU+^zoSWhltMyd^X}-EIS2_ghJQysVecqMQkxS4slN-yl{X9vO%2hNm>Ptf@3WI3E~4M5#C9(msRK#=SCQS>$pKHsSd?f@+PBW z^i{q>fBFf@vUW|I8q2qkdW(ttSPKIa(eX_5Gj_GG_chX;`Ti&Ck;psrb?ie=);00& z*Z1)jj=n*BJtFqKXW3{Q$~gicJ|qZAo&Q~&jS|KQT4#BKFQE_9^xwe%9$*j;k#57j zz?@NDm1*2$B?Cp2aEp`(7q>CPF@bx`nkAVO=J>xwI(O+;gu}IaoR#UxAhxN#EV>Xs zkrx|CZXlK Date: Sun, 11 Jan 2026 12:13:23 -0500 Subject: [PATCH 2/2] Completed Contiguous Array HW Problem --- ContiguousArray.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ContiguousArray.java diff --git a/ContiguousArray.java b/ContiguousArray.java new file mode 100644 index 00000000..84b0387c --- /dev/null +++ b/ContiguousArray.java @@ -0,0 +1,26 @@ +import java.util.HashMap; + +public class ContiguousArray { + public int findMaxLength(int[] nums) { + if(nums == null || nums.length == 0) return 0; + //Create HashMap to store running sum & corresponding index + HashMap map = new HashMap<>(); + int rSum = 0; + int max = 0; + map.put(0,-1); //create dummy entry in map to catch initial ssubarray + for(int i = 0; i < nums.length; i++){ + if(nums[i] == 0){ + rSum = rSum - 1; + } else { + rSum = rSum + 1; + } + if(map.containsKey(rSum)){ + int curr = i - map.get(rSum); + max = Math.max(max, curr); + } else { + map.put(rSum, i); + } + } + return max; + } +}