| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.CheckIfArrayPairsAreDivisibleByK; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed 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) |
| | 8 | 25 | | { |
| | 8 | 26 | | var countDictionary = new Dictionary<int, int>(); |
| | | 27 | | |
| | 116 | 28 | | foreach (var item in arr) |
| | 46 | 29 | | { |
| | 46 | 30 | | var remainder = ((item % k) + k) % k; |
| | | 31 | | |
| | 46 | 32 | | if (!countDictionary.TryAdd(remainder, 1)) |
| | 14 | 33 | | { |
| | 14 | 34 | | countDictionary[remainder]++; |
| | 14 | 35 | | } |
| | 46 | 36 | | } |
| | | 37 | | |
| | 70 | 38 | | foreach (var keyValuePair in countDictionary) |
| | 24 | 39 | | { |
| | 24 | 40 | | if (keyValuePair.Key == 0) |
| | 3 | 41 | | { |
| | 3 | 42 | | if (keyValuePair.Value % 2 != 0) |
| | 0 | 43 | | { |
| | 0 | 44 | | return false; |
| | | 45 | | } |
| | 3 | 46 | | } |
| | | 47 | | else |
| | 21 | 48 | | { |
| | 21 | 49 | | if (countDictionary.TryGetValue(k - keyValuePair.Key, out var count) && count == keyValuePair.Value) |
| | 19 | 50 | | { |
| | 19 | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 2 | 54 | | return false; |
| | | 55 | | } |
| | 3 | 56 | | } |
| | | 57 | | |
| | 6 | 58 | | return true; |
| | 8 | 59 | | } |
| | | 60 | | } |