< Summary

Information
Class: LeetCode.Algorithms.CountOfInterestingSubarrays.CountOfInterestingSubarraysPrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountOfInterestingSubarrays\CountOfInterestingSubarraysPrefixSum.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 59
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
CountInterestingSubarrays(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountOfInterestingSubarrays\CountOfInterestingSubarraysPrefixSum.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.CountOfInterestingSubarrays;
 13
 14/// <inheritdoc />
 15public class CountOfInterestingSubarraysPrefixSum : ICountOfInterestingSubarrays
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(modulo)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="modulo"></param>
 23    /// <param name="k"></param>
 24    /// <returns></returns>
 25    public long CountInterestingSubarrays(IList<int> nums, int modulo, int k)
 326    {
 327        long result = 0;
 28
 329        var prefixModDictionary = new Dictionary<int, int>
 330        {
 331            [0] = 1
 332        };
 33
 334        var prefix = 0;
 35
 3136        foreach (var num in nums)
 1137        {
 1138            if (num % modulo == k)
 739            {
 740                prefix++;
 741            }
 42
 1143            var currentMod = prefix % modulo;
 1144            var target = (currentMod - k + modulo) % modulo;
 45
 1146            if (prefixModDictionary.TryGetValue(target, out var count))
 947            {
 948                result += count;
 949            }
 50
 1151            if (!prefixModDictionary.TryAdd(currentMod, 1))
 552            {
 553                prefixModDictionary[currentMod]++;
 554            }
 1155        }
 56
 357        return result;
 358    }
 59}