< Summary

Information
Class: LeetCode.Algorithms.SubarraySumsDivisibleByK.SubarraySumsDivisibleByKDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SubarraySumsDivisibleByK\SubarraySumsDivisibleByKDictionary.cs
Line coverage
85%
Covered lines: 17
Uncovered lines: 3
Coverable lines: 20
Total lines: 55
Line coverage: 85%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SubarraysDivByK(...)83.33%6685%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SubarraySumsDivisibleByK\SubarraySumsDivisibleByKDictionary.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.SubarraySumsDivisibleByK;
 13
 14/// <inheritdoc />
 15public class SubarraySumsDivisibleByKDictionary : ISubarraySumsDivisibleByK
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(k)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int SubarraysDivByK(int[] nums, int k)
 925    {
 926        var result = 0;
 27
 928        var prefixModDictionary = new Dictionary<int, int> { { 0, 1 } };
 29
 930        var sum = 0;
 31
 6932        foreach (var num in nums)
 2133        {
 2134            sum += num;
 35
 2136            var prefixMod = sum % k;
 37
 2138            if (prefixMod < 0)
 039            {
 040                prefixMod += k;
 041            }
 42
 2143            if (prefixModDictionary.TryAdd(prefixMod, 1))
 344            {
 345                continue;
 46            }
 47
 1848            result += prefixModDictionary[prefixMod];
 49
 1850            prefixModDictionary[prefixMod]++;
 1851        }
 52
 953        return result;
 954    }
 55}