< Summary

Information
Class: LeetCode.Algorithms.FindKthBitInNthBinaryString.FindKthBitInNthBinaryStringRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindKthBitInNthBinaryString\FindKthBitInNthBinaryStringRecursive.cs
Line coverage
87%
Covered lines: 14
Uncovered lines: 2
Coverable lines: 16
Total lines: 49
Line coverage: 87.5%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindKthBit(...)87.5%8887.5%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindKthBitInNthBinaryString\FindKthBitInNthBinaryStringRecursive.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.FindKthBitInNthBinaryString;
 13
 14/// <inheritdoc />
 15public class FindKthBitInNthBinaryStringRecursive : IFindKthBitInNthBinaryString
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public char FindKthBit(int n, int k)
 725    {
 726        if (n == 1)
 227        {
 228            return '0';
 29        }
 30
 531        var length = (1 << n) - 1;
 532        var mid = (length / 2) + 1;
 33
 534        if (k == mid)
 035        {
 036            return '1';
 37        }
 38
 539        if (k < mid)
 240        {
 241            return FindKthBit(n - 1, k);
 42        }
 43
 344        var mirroredIndex = length - k + 1;
 345        var mirroredBit = FindKthBit(n - 1, mirroredIndex);
 46
 347        return mirroredBit == '0' ? '1' : '0';
 748    }
 49}