From 979c679de7d6be4d51b6b02a9f27fe38b5cd4880 Mon Sep 17 00:00:00 2001 From: Utkarsh-Aggarwal Date: Sat, 19 Oct 2024 11:47:02 +0530 Subject: [PATCH] Description of changes --- october_2024/potd_19_10_2024.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 october_2024/potd_19_10_2024.cpp diff --git a/october_2024/potd_19_10_2024.cpp b/october_2024/potd_19_10_2024.cpp new file mode 100644 index 0000000..6602767 --- /dev/null +++ b/october_2024/potd_19_10_2024.cpp @@ -0,0 +1,29 @@ +/* + +class Solution { +public: + char findKthBit(int n, int k) { + int invertCount = 0; + int len = (1 << n) - 1; // Length of Sn is 2^n - 1 + + while (k > 1) { + // If k is in the middle, return based on inversion count + if (k == len / 2 + 1) { + return invertCount % 2 == 0 ? '1' : '0'; + } + + // If k is in the second half, invert and mirror + if (k > len / 2) { + k = len + 1 - k; // Mirror position + invertCount++; // Increment inversion count + } + + len /= 2; // Reduce length for next iteration + } + + // For the first position, return based on inversion count + return invertCount % 2 == 0 ? '0' : '1'; + } +}; + +*/ \ No newline at end of file