< Summary

Information
Class: LeetCode.Algorithms.CheckIfArrayPairsAreDivisibleByK.CheckIfArrayPairsAreDivisibleByKDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CheckIfArrayPairsAreDivisibleByK\CheckIfArrayPairsAreDivisibleByKDictionary.cs
Line coverage
92%
Covered lines: 24
Uncovered lines: 2
Coverable lines: 26
Total lines: 60
Line coverage: 92.3%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanArrange(...)92.85%141492.3%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CheckIfArrayPairsAreDivisibleByK\CheckIfArrayPairsAreDivisibleByKDictionary.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.CheckIfArrayPairsAreDivisibleByK;
 13
 14/// <inheritdoc />
 15public class CheckIfArrayPairsAreDivisibleByKDictionary : ICheckIfArrayPairsAreDivisibleByK
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public bool CanArrange(int[] arr, int k)
 825    {
 826        var countDictionary = new Dictionary<int, int>();
 27
 11628        foreach (var item in arr)
 4629        {
 4630            var remainder = ((item % k) + k) % k;
 31
 4632            if (!countDictionary.TryAdd(remainder, 1))
 1433            {
 1434                countDictionary[remainder]++;
 1435            }
 4636        }
 37
 7038        foreach (var keyValuePair in countDictionary)
 2439        {
 2440            if (keyValuePair.Key == 0)
 341            {
 342                if (keyValuePair.Value % 2 != 0)
 043                {
 044                    return false;
 45                }
 346            }
 47            else
 2148            {
 2149                if (countDictionary.TryGetValue(k - keyValuePair.Key, out var count) && count == keyValuePair.Value)
 1950                {
 1951                    continue;
 52                }
 53
 254                return false;
 55            }
 356        }
 57
 658        return true;
 859    }
 60}