mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
add codeforces solutions folder
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
class Solution{
|
||||
public:
|
||||
char findKthBit(int n, int k){
|
||||
int invertCount = 0;
|
||||
int len = (1<<n)-1;
|
||||
while(k>1){
|
||||
if(k==len/2+1){
|
||||
return invertCount % 2==0? '1' : '0';
|
||||
|
||||
}
|
||||
if(k>len/2){
|
||||
k = len-k+1;
|
||||
invertCount++;
|
||||
}
|
||||
len/=2;
|
||||
}
|
||||
return invertCount%2==0?'0':'1';
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
//TC: o(n) , SC : o(1)
|
||||
|
||||
/*
|
||||
Approach:
|
||||
|
||||
We do NOT build the full string Sn because its length grows exponentially (2^n - 1).
|
||||
Instead, we use the recursive structure:
|
||||
|
||||
Sn = S(n-1) + "1" + reverse(invert(S(n-1)))
|
||||
|
||||
For any position k:
|
||||
1) If k lies in the left half:
|
||||
→ It directly corresponds to the same position in S(n-1).
|
||||
|
||||
2) If k is the middle element:
|
||||
→ The value is always '1'.
|
||||
|
||||
3) If k lies in the right half:
|
||||
→ The right half is reverse(invert(S(n-1))).
|
||||
→ Mirror the index into the left half.
|
||||
→ Remember that the value must be inverted.
|
||||
|
||||
We keep reducing the problem from Sn → S(n-1) until we reach
|
||||
the base case S1 = "0".
|
||||
|
||||
The final answer depends on how many times we entered
|
||||
the right half (i.e., how many inversions occurred).
|
||||
|
||||
Time Complexity: O(n)
|
||||
Space Complexity: O(1) (iterative version)
|
||||
*/
|
||||
@@ -0,0 +1,17 @@
|
||||
class Solution{
|
||||
public:
|
||||
char findKthBit(int n, int k){
|
||||
string seq = "0";
|
||||
for(int i = 1;i<n && seq.length() < k;++i){
|
||||
seq+='1';
|
||||
string temp = seq;
|
||||
for(int j=temp.length()-2;j>=0;--j){
|
||||
char invertedBit = (temp[j]=='1')? '0': '1';
|
||||
seq+=invertedBit;
|
||||
}
|
||||
}
|
||||
return seq[k-1];
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
class Solution{
|
||||
public:
|
||||
char findKthBit(int n, int k){
|
||||
if(n==1) return '0';
|
||||
int len = 1<<n;
|
||||
if(k<len/2) {
|
||||
return findKthBit(n-1,k);
|
||||
|
||||
}
|
||||
else if(k == len/2){
|
||||
return '1';
|
||||
}
|
||||
else{
|
||||
char correspondingBit = findKthBit(n-1, len-k);
|
||||
return ((correspondingBit == '0')? '1':'0');
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
Recursive Idea:
|
||||
|
||||
The string is defined as:
|
||||
Sn = S(n-1) + "1" + reverse(invert(S(n-1)))
|
||||
|
||||
Instead of building the full string (which grows exponentially),
|
||||
we determine where the k-th position lies:
|
||||
|
||||
1) If k is in the left half:
|
||||
→ It directly corresponds to the same position in S(n-1).
|
||||
→ So recurse: findKthBit(n-1, k)
|
||||
|
||||
2) If k is exactly the middle:
|
||||
→ The middle element is always '1'.
|
||||
|
||||
3) If k is in the right half:
|
||||
→ The right half is reverse(invert(S(n-1))).
|
||||
→ Mirror the index using (len - k).
|
||||
→ Recursively find that bit in S(n-1).
|
||||
→ Invert the result.
|
||||
|
||||
Each recursive call reduces n by 1,
|
||||
tracing the origin of the bit back to the base case S1 = "0".
|
||||
|
||||
Time Complexity: O(n)
|
||||
Space Complexity: O(n) (recursion stack)
|
||||
*/
|
||||
|
||||
// TC: o(n) and SC : o(n)
|
||||
Reference in New Issue
Block a user