Skip to main content

Maximum Size Subarray Sum Equals k

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 273 ms)​

MetricValue
Runtime273 ms
Memory54.1 MB
Date2022-01-22
Solution
public class Solution {
public int MaxSubArrayLen(int[] nums, int k) {
Dictionary<int, int> map = new Dictionary<int, int>();
int max = 0, sum = 0;
map.Add(0,-1);
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
if(map.ContainsKey(sum-k))
{
max = Math.Max(max, i-map[sum-k]);
}
if(!map.ContainsKey(sum))
{
map.Add(sum, i);
}


}
return max;
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed