< Summary

Information
Class: LeetCode.Algorithms.SmallestNumberWithAllSetBits.SmallestNumberWithAllSetBitsBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SmallestNumberWithAllSetBits\SmallestNumberWithAllSetBitsBinarySearch.cs
Line coverage
91%
Covered lines: 21
Uncovered lines: 2
Coverable lines: 23
Total lines: 54
Line coverage: 91.3%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
SmallestNumber(...)66.66%6681.81%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SmallestNumberWithAllSetBits\SmallestNumberWithAllSetBitsBinarySearch.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.SmallestNumberWithAllSetBits;
 13
 14/// <inheritdoc />
 15public sealed class SmallestNumberWithAllSetBitsBinarySearch : ISmallestNumberWithAllSetBits
 16{
 117    private static readonly int[] Numbers =
 118    [
 119        3,
 120        7,
 121        15,
 122        31,
 123        63,
 124        127,
 125        255,
 126        511,
 127        1023
 128    ];
 29
 30    /// <summary>
 31    ///     Time complexity - O(log n)
 32    ///     Space complexity - O(1)
 33    /// </summary>
 34    /// <param name="n"></param>
 35    /// <returns></returns>
 36    public int SmallestNumber(int n)
 337    {
 338        if (n == 1)
 039        {
 040            return 1;
 41        }
 42
 343        var index = Array.BinarySearch(Numbers, n);
 44
 345        if (index >= 0)
 146        {
 147            return Numbers[index];
 48        }
 49
 250        index = ~index;
 51
 252        return index < Numbers.Length ? Numbers[index] : Numbers[^1];
 353    }
 54}