< Summary

Information
Class: LeetCode.Algorithms.DivideArrayInSetsOfKConsecutiveNumbers.DivideArrayInSetsOfKConsecutiveNumbersDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DivideArrayInSetsOfKConsecutiveNumbers\DivideArrayInSetsOfKConsecutiveNumbersDictionary.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsPossibleDivide(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DivideArrayInSetsOfKConsecutiveNumbers\DivideArrayInSetsOfKConsecutiveNumbersDictionary.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.DivideArrayInSetsOfKConsecutiveNumbers;
 13
 14/// <inheritdoc />
 15public class DivideArrayInSetsOfKConsecutiveNumbersDictionary : IDivideArrayInSetsOfKConsecutiveNumbers
 16{
 17    public bool IsPossibleDivide(int[] nums, int k)
 1518    {
 1519        if (nums.Length % k != 0)
 320        {
 321            return false;
 22        }
 23
 1224        Array.Sort(nums);
 25
 1226        var numsDictionary = new Dictionary<int, int>();
 27
 16828        foreach (var num in nums)
 6629        {
 6630            if (!numsDictionary.TryAdd(num, 1))
 1031            {
 1032                numsDictionary[num]++;
 1033            }
 6634        }
 35
 3336        while (numsDictionary.Count > 0)
 2337        {
 2338            var firstNum = numsDictionary.First().Key;
 39
 16840            for (var i = 0; i < k; i++)
 6341            {
 6342                var currentNum = firstNum + i;
 43
 6344                if (numsDictionary.TryGetValue(currentNum, out var value))
 6145                {
 6146                    if (value == 1)
 5347                    {
 5348                        numsDictionary.Remove(currentNum);
 5349                    }
 50                    else
 851                    {
 852                        numsDictionary[currentNum] = value - 1;
 853                    }
 6154                }
 55                else
 256                {
 257                    return false;
 58                }
 6159            }
 2160        }
 61
 1062        return true;
 1563    }
 64}