Nitin Singh’s Post

🚀 DSA Challenge – Day 88 Problem: Count Subarrays with Sum Equal to K 📊🔥 This problem was an excellent exercise in using prefix sums and hashmaps to efficiently track cumulative sums and identify subarrays that meet a specific target. 🧠 Problem Summary: Given an array of integers nums and an integer k, find the total number of contiguous subarrays whose sum equals k. ⚙️ My Approach: 1️⃣ Maintain a running prefix sum s as we iterate through the array. 2️⃣ Use a hashmap hashMap to store how many times each prefix sum has appeared. 3️⃣ For each element, check if (s - k) exists in the hashmap — if yes, it means a subarray summing to k ends at the current index. 4️⃣ Update the hashmap with the new prefix sum count. 📈 Complexity: Time: O(n) — each element is processed once. Space: O(n) — for storing prefix sums in the hashmap. ✨ Key Takeaway: Prefix sum combined with a hashmap is a powerful technique to convert subarray problems from O(n²) brute force into elegant O(n) solutions. ⚡ 🔖 #DSA #100DaysOfCode #LeetCode #ProblemSolving #HashMap #PrefixSum #CodingChallenge #Python #Algorithms #EfficientCode #InterviewPrep #TechCommunity #CodeEveryday #LearningByBuilding

  • text

Absolutely amazing 👏👏

Like
Reply

To view or add a comment, sign in

Explore content categories