Day 17 DSA Journey: Prefix Sum & HashMap Optimization

🚀 Day 17 of my DSA Journey (LeetCode + Java) Today was all about Prefix Sum, Two-Pass Array Logic, and HashMap-based subarray counting. All three problems helped me think more optimally and understand how preprocessing reduces time complexity. ✅ Problems Solved Today: LC 303 – Range Sum Query: Immutable LC 238 – Product of Array Except Self LC 560 – Subarray Sum Equals K 🧠 My Experience Solving These: 🔸 303. Range Sum Query – Immutable This problem looked simple, but the real trick was understanding how to build logic using a constructor. I learned how prefix sum converts multiple queries into O(1) time. Precomputing prefix sums in the constructor gave me a real-world feel of initialization + efficient querying. 🔸 238. Product of Array Except Self Initially, I solved it in a less optimal way. Then I changed my mindset to the optimal approach: ✔ Left pass → store product before index ✔ Right pass → multiply product after index ✔ No division, no extra space (only result array) After that shift in thinking, the solution became very clean and efficient. 🔸 560. Subarray Sum Equals K This was the most insightful problem of the day. Using a HashMap + prefix sum, I learned how to find subarrays that sum to K in O(n) time: ✔ Maintain cumulative sum ✔ Check if (sum − k) already occurred ✔ If yes → increment count ✔ Store frequencies for later use A beautiful example of how maps help optimize brute-force logic. 📌 Key Takeaway Today: Prefix sums and maps are extremely powerful when combined. With the right preprocessing and logic: O(n²) problems become O(n) Hard problems become intuitive Logic becomes cleaner and more structured Every day, solving new patterns is improving my confidence and problem-solving instincts. 💪 Step-by-step, getting better. On to Day 18! 🚀 #DSA #LeetCode #Java #PrefixSum #HashMap #ProblemSolving #CodingJourney #Consistency #LearningDaily

To view or add a comment, sign in

Explore content categories