-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution523.cs
47 lines (40 loc) · 1.44 KB
/
Solution523.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution523
{
/// <summary>
/// 523. Continuous Subarray Sum - Medium
/// <a href="https://leetcode.com/problems/continuous-subarray-sum">See the problem</a>
/// </summary>
public bool CheckSubarraySum(int[] nums, int k)
{
// Dictionary to store (remainder, index)
var remainderIndexMap = new Dictionary<int, int>();
remainderIndexMap[0] = -1; // Initialize with remainder 0 at index -1 to handle edge cases
int prefixSum = 0;
for (int i = 0; i < nums.Length; i++)
{
prefixSum += nums[i];
// Calculate remainder of prefixSum divided by k
int remainder = prefixSum % k;
// Adjust for negative remainders to ensure positive index usage
if (remainder < 0) remainder += k;
// Check if this remainder has been seen before
if (remainderIndexMap.ContainsKey(remainder))
{
// Ensure the subarray length is at least 2
if (i - remainderIndexMap[remainder] > 1)
{
return true;
}
}
else
{
// Store the remainder with the corresponding index
remainderIndexMap[remainder] = i;
}
}
// If no valid subarray found
return false;
}
}