Subarray Sum Equals K: Prefix Sum and HashMap Approach

Day 40/100 | #100DaysOfDSA 💻🔥 Today’s problem: Subarray Sum Equals K Goal: Find the total number of subarrays whose sum equals k. Approach: Prefix Sum + HashMap Idea: • Keep track of the running sum while traversing the array • If (currentSum - k) was seen before, a subarray with sum k exists • Use a HashMap to store prefix sums and their frequencies Key insight: If prefixSum[j] - prefixSum[i] = k, then the subarray between them sums to k. Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Prefix sums combined with hashing can turn many subarray problems from O(n²) to O(n). Day 40 — still stacking patterns. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #PrefixSum #HashMap #Java #CodingJourney #InterviewPrep #ProblemSolving

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories