Output Example
title: “Subarray Sum Equals K” slug: subarray-sum-equals-k
My Solution
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int,int> mp;
int len=nums.size();
mp[0]=1;
int prefix=0;
int count=0;
int sum=0;
for(int i=0;i<len;i++){
prefix=prefix+nums[i];
sum=prefix-k;
count=count+mp[sum];
mp[prefix]+=1;
}
return count;
}
};Submission Review
Approach
- Technique: Prefix Sum with Hash Map (Frequency Map).
- Optimality: Optimal. It achieves time complexity by transforming the range sum problem into a difference lookup, effectively finding subarrays where .
Complexity
- Time Complexity: average, where is the length of
nums, asunordered_mapoperations take on average. - Space Complexity: in the worst case to store up to unique prefix sums in the hash map.
Efficiency Feedback
- Performance: The logic is efficient. Using
std::unordered_mapis generally fine, but in scenarios with strict time limits or malicious test cases,std::unordered_mapcan suffer from collisions ( worst case). - Optimization: If the constraints on
numsvalues are small, astd::vectoror a custom hash table could be used. For competitive programming,std::mapis and safer against anti-hash tests.
Code Quality
- Readability: Good. The logic is concise and follows standard idiomatic C++.
- Structure: Good. The initialization
mp[0]=1correctly handles subarrays starting from index 0. - Naming: Moderate.
prefixandsumvariables are slightly redundant;current_sumandtargetmight be more descriptive. - Concrete Improvements:
- Redundancy: The variable
sumis unnecessary. You can directly performcount += mp[prefix - k]. - Performance: Reserve space for the map if the input size is large:
mp.reserve(len);to avoid rehashing overhead. - Refactored snippet:
unordered_map<int, int> mp; mp.reserve(nums.size()); mp[0] = 1; int current_sum = 0, count = 0; for (int x : nums) { current_sum += x; if (mp.count(current_sum - k)) count += mp[current_sum - k]; mp[current_sum]++; }
- Redundancy: The variable
Question Revision
Revision Report: Subarray Sum Equals K
Pattern: Prefix Sum + Hash Map
Brute Force: Generate all possible subarrays using nested loops, calculate their sums, and compare each against k.
- Time Complexity:
- Space Complexity:
Optimal Approach: Use a running prefix sum to track the current total. Store the frequency of each prefix sum encountered so far in a hash map. If (current_sum - k) exists in the map, it means there is a subarray ending at the current index that sums to k.
- Time Complexity:
- Space Complexity:
The ‘Aha’ Moment: The problem asks for the number of subarrays that sum to a target, implying you need a way to look back at previous prefix sums to find the difference k instantly.
Summary: When asked for the count of subarrays with a specific sum, map the frequency of prefix sums to instantly identify valid segments.