< Summary

Information
Class: LeetCode.Algorithms.ContinuousSubarraySum.ContinuousSubarraySumDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ContinuousSubarraySum\ContinuousSubarraySumDictionary.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 53
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CheckSubarraySum(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ContinuousSubarraySum\ContinuousSubarraySumDictionary.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.ContinuousSubarraySum;
 13
 14/// <inheritdoc />
 15public class ContinuousSubarraySumDictionary : IContinuousSubarraySum
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n), where n is the length of the input array nums
 19    ///     Space complexity - O(min(n,k)), where n is the length of the input array nums and k is the number of possibl
 20    ///     remainders when taking modulo k
 21    /// </summary>
 22    /// <param name="nums"></param>
 23    /// <param name="k"></param>
 24    /// <returns></returns>
 25    public bool CheckSubarraySum(int[] nums, int k)
 326    {
 327        var remainderDictionary = new Dictionary<int, int> { [0] = -1 };
 28
 329        var sum = 0;
 30
 2631        for (var i = 0; i < nums.Length; i++)
 1232        {
 1233            sum += nums[i];
 34
 1235            if (k != 0)
 1236            {
 1237                sum %= k;
 1238            }
 39
 1240            if (remainderDictionary.TryAdd(sum, i))
 941            {
 942                continue;
 43            }
 44
 345            if (i - remainderDictionary[sum] > 1)
 246            {
 247                return true;
 48            }
 149        }
 50
 151        return false;
 352    }
 53}