< Summary

Information
Class: LeetCode.Algorithms.ReorderedPowerOfTwo.ReorderedPowerOfTwoFrequencyArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReorderedPowerOfTwo\ReorderedPowerOfTwoFrequencyArray.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ReorderedPowerOf2(...)100%44100%
GetDigitsFrequency(...)100%22100%
AreEqual(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReorderedPowerOfTwo\ReorderedPowerOfTwoFrequencyArray.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.ReorderedPowerOfTwo;
 13
 14/// <inheritdoc />
 15public class ReorderedPowerOfTwoFrequencyArray : IReorderedPowerOfTwo
 16{
 17    /// <summary>
 18    ///     Time complexity - O(1)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <returns></returns>
 23    public bool ReorderedPowerOf2(int n)
 1324    {
 1325        var targetDigitsFrequency = GetDigitsFrequency(n);
 26
 44827        for (var i = 0; i < 31; i++)
 22028        {
 22029            var currentDigitsFrequency = GetDigitsFrequency(1 << i);
 30
 22031            if (AreEqual(targetDigitsFrequency, currentDigitsFrequency))
 932            {
 933                return true;
 34            }
 21135        }
 36
 437        return false;
 1338    }
 39
 40    private static int[] GetDigitsFrequency(int num)
 23341    {
 23342        var digitsFrequency = new int[10];
 43
 126444        while (num > 0)
 103145        {
 103146            digitsFrequency[num % 10]++;
 47
 103148            num /= 10;
 103149        }
 50
 23351        return digitsFrequency;
 23352    }
 53
 54    private static bool AreEqual(int[] a, int[] b)
 22055    {
 82256        for (var i = 0; i < 10; i++)
 40257        {
 40258            if (a[i] != b[i])
 21159            {
 21160                return false;
 61            }
 19162        }
 63
 964        return true;
 22065    }
 66}