Skip to content

Commit 979c679

Browse files
Description of changes
1 parent 57152a5 commit 979c679

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

october_2024/potd_19_10_2024.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
3+
class Solution {
4+
public:
5+
char findKthBit(int n, int k) {
6+
int invertCount = 0;
7+
int len = (1 << n) - 1; // Length of Sn is 2^n - 1
8+
9+
while (k > 1) {
10+
// If k is in the middle, return based on inversion count
11+
if (k == len / 2 + 1) {
12+
return invertCount % 2 == 0 ? '1' : '0';
13+
}
14+
15+
// If k is in the second half, invert and mirror
16+
if (k > len / 2) {
17+
k = len + 1 - k; // Mirror position
18+
invertCount++; // Increment inversion count
19+
}
20+
21+
len /= 2; // Reduce length for next iteration
22+
}
23+
24+
// For the first position, return based on inversion count
25+
return invertCount % 2 == 0 ? '0' : '1';
26+
}
27+
};
28+
29+
*/

0 commit comments

Comments
 (0)