From de7b8ffed0dbe4174e2f2860a16c5bec9b839d96 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Fri, 2 Jan 2026 13:49:12 -0600 Subject: [PATCH 1/3] Add subarraySum method to calculate target sums Implement subarray sum function using a hashmap to count occurrences of sums. --- Leetcode_560.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Leetcode_560.java diff --git a/Leetcode_560.java b/Leetcode_560.java new file mode 100644 index 00000000..2615397d --- /dev/null +++ b/Leetcode_560.java @@ -0,0 +1,26 @@ +//As they are asking subarray sum, have a variable and maintain sum in it at every index. At every index, we need some amount to match with target. That amount can be calculated as sum-target. +//To check whether we have any sum-target in the array, we will maintain a map and count number of occurances of quantity(sum) at every index. If it's present, that many times we have the possibility to reach target. +//Tc: O(n) +//Sc: O(n) + +class Solution { + public int subarraySum(int[] nums, int k) { + Map map=new HashMap<>(); + map.put(0,1); + int count=0; + int sum=0; + + for(int i:nums){ + sum+=i; + int target=sum-k; + if(map.containsKey(target)){ + count+=map.get(target); + } + + map.put(sum,map.getOrDefault(sum,0)+1); + } + + return count; + + } +} From 1e96f796efc16d428f9b715d5dd7ef910ab8d74e Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sun, 4 Jan 2026 01:25:58 -0600 Subject: [PATCH 2/3] Add solution for longest palindrome problem Implement solution to find longest palindrome length. --- Leetcode_409.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Leetcode_409.java diff --git a/Leetcode_409.java b/Leetcode_409.java new file mode 100644 index 00000000..4b541604 --- /dev/null +++ b/Leetcode_409.java @@ -0,0 +1,26 @@ +//Rule: Lookslike a mirror immage. i.e., replica. So, we should have same characters at both front side and back side. So, there should be one char with odd times and rest all should be even times. +//Solving it using set +//TC: O(n); SC:O(n) +class Solution { + public int longestPalindrome(String s) { + Set set=new HashSet<>(); + + int ans=0; + + for(int i=0;i0) ans+=1; + + return ans; + + } +} From 4e8776608f79dfb8c76994444003191394cf2bbf Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sun, 4 Jan 2026 01:26:59 -0600 Subject: [PATCH 3/3] Add solution for Leetcode problem 525 Implement solution to find maximum length of contiguous subarray with equal number of 0's and 1's using prefix sum and hashmap. --- Leetcode_525.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Leetcode_525.java diff --git a/Leetcode_525.java b/Leetcode_525.java new file mode 100644 index 00000000..65f7ec82 --- /dev/null +++ b/Leetcode_525.java @@ -0,0 +1,30 @@ +//Rule: Contiguous array-->Number of 1's is equals to number of 0's. +//Let's consider 1's as increments and 0 as decrements. As number of 1's = number of 0's, number of increments is equals to number of decrements. +//The indexes at which prefix sum is equals will have equal number of 0's and 1's between them. +//prefix sum is calculated using sum variable. And traking the indexes with similar sum using hashmap +//TC: O(n); SC: O(n) +class Solution { + public int findMaxLength(int[] nums) { + Map map=new HashMap<>(); + map.put(0,-1); + int sum=0; + int ans=0; + + for(int i=0;i