Java Solution: Continuous Subarray Sum with Prefix Sum and HashMap

Day 99/100 – #100DaysOfCode 🚀 | #Java #PrefixSum #HashMap ✅ Problem Solved: Continuous Subarray Sum (LeetCode 523) 🧩 Problem Summary: Given an integer array and an integer k, determine if the array has a continuous subarray of size at least 2 whose sum is a multiple of k. 💡 Approach Used: ✔ Used Prefix Sum + HashMap ✔ Key observation: If two prefix sums have the same remainder when divided by k, the subarray between them has a sum divisible by k. Steps: Maintain running sum Store (prefixSum % k) → earliest index in a HashMap If the same remainder appears again and the subarray length ≥ 2 → return true Initialize map with (0 → -1) to handle edge cases ⚙ Time Complexity: O(N) 📦 Space Complexity: O(K) (where K = number of unique remainders) ✨ Takeaway: Modular arithmetic paired with prefix sums is a powerful pattern for detecting divisible subarrays in linear time. #Java #LeetCode #PrefixSum #HashMap #100DaysOfCode #CodingChallenge

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories