From d4854ffe1805d42d42507a0519460d5658523855 Mon Sep 17 00:00:00 2001 From: subbu4061 Date: Mon, 12 Jan 2026 18:15:27 -0800 Subject: [PATCH] Completed Hashing-2 problems --- Subarray sum equals k.java | 28 ++++++++++++++++++++++++++++ contiguous array.java | 24 ++++++++++++++++++++++++ longest palindrome.java | 24 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 Subarray sum equals k.java create mode 100644 contiguous array.java create mode 100644 longest palindrome.java diff --git a/Subarray sum equals k.java b/Subarray sum equals k.java new file mode 100644 index 00000000..9170bc12 --- /dev/null +++ b/Subarray sum equals k.java @@ -0,0 +1,28 @@ +// TimeComplexity: O(n) +// SpaceComplexity: O(n) +// Did this code successfully run on Leetcode : yes +// Three sentences abou the solution: We count the number of subarrays whose sum equals k using a prefix-sum approach.It keeps a running sum and uses a HashMap to store how many times each prefix sum has occurred. +// If (currentSum − k) exists in the map, it means a subarray ending at the current index sums to k. +// The map is updated at each step with the current prefix sum count. Finally, we return the total number of such subarrays found. + +class Solution { + public int subarraySum(int[] nums, int k) { + int count =0; + int sum =0; + HashMap map = new HashMap<>(); + map.put(0,1); + for(int i=0; i map = new HashMap<>(); + map.put(0,-1); + for(int i=0; i set = new HashSet<>(); + int count =0; + for(int i=0; i