Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 1545. Find Kth Bit in Nth Binary String #613

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 1545. Find Kth Bit in Nth Binary String
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
char solve(int k,int n, vector<int> & arr){
if(k==0)return '0';
if(k==1)return '1';
int i=0;
for(i=0;i<n-1;i++){
if( arr[i]>k)break;
}
i--;
if(k==arr[i])return '1';
char rec=solve(arr[i]-(k-arr[i]),n,arr);
if(rec=='0')return '1';
return '0';
}
char findKthBit(int n, int k) {
int temp=0;
vector<int> arr;
for(int i=0;i< n;i++){
arr.push_back(2*temp+1);
temp=arr.back();
}
return solve(k-1,n,arr);
}
};
Loading