Bitmasking and searching consecutive 1's
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have written code to count the continuous 1's in an array or you can use binary string for convenience. Any solution using the binary string is acceptable.
My Solution is>>
#include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int main()
int n,q,k,count;
string str;
cin>>n>>q>>k;
bitset<MAX>s,c;
//inserting bits in array
for(int i=0;i<n;i++)
int temp;
cin>>temp;
s[i]=temp;
cin>>str;
for(int i=0;i<q;i++)
//making duplicate bitset
c=s;
if(str[i]=='?')
count=0;
while(c!=0)
//using bitmask to count maximum no of continuous 1's-O(1's bit)
c=(c&(c<<1));
count++;
if(count>k)
cout<<k<<"n";
else
cout<<count<<"n";
else
//shifting each bit to right and updating first bit with previous last
// bit
bool lb=s[n-1];
s=s>>1;
s[n-1]=lb;
c++ programming-challenge binary-search bitset
New contributor
add a comment |Â
up vote
1
down vote
favorite
I have written code to count the continuous 1's in an array or you can use binary string for convenience. Any solution using the binary string is acceptable.
My Solution is>>
#include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int main()
int n,q,k,count;
string str;
cin>>n>>q>>k;
bitset<MAX>s,c;
//inserting bits in array
for(int i=0;i<n;i++)
int temp;
cin>>temp;
s[i]=temp;
cin>>str;
for(int i=0;i<q;i++)
//making duplicate bitset
c=s;
if(str[i]=='?')
count=0;
while(c!=0)
//using bitmask to count maximum no of continuous 1's-O(1's bit)
c=(c&(c<<1));
count++;
if(count>k)
cout<<k<<"n";
else
cout<<count<<"n";
else
//shifting each bit to right and updating first bit with previous last
// bit
bool lb=s[n-1];
s=s>>1;
s[n-1]=lb;
c++ programming-challenge binary-search bitset
New contributor
1
Try to give an example with possible value and invalid inputs and expected outputs. it will help people to figure out the way and the purpose of your programme.
â Calak
1 hour ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have written code to count the continuous 1's in an array or you can use binary string for convenience. Any solution using the binary string is acceptable.
My Solution is>>
#include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int main()
int n,q,k,count;
string str;
cin>>n>>q>>k;
bitset<MAX>s,c;
//inserting bits in array
for(int i=0;i<n;i++)
int temp;
cin>>temp;
s[i]=temp;
cin>>str;
for(int i=0;i<q;i++)
//making duplicate bitset
c=s;
if(str[i]=='?')
count=0;
while(c!=0)
//using bitmask to count maximum no of continuous 1's-O(1's bit)
c=(c&(c<<1));
count++;
if(count>k)
cout<<k<<"n";
else
cout<<count<<"n";
else
//shifting each bit to right and updating first bit with previous last
// bit
bool lb=s[n-1];
s=s>>1;
s[n-1]=lb;
c++ programming-challenge binary-search bitset
New contributor
I have written code to count the continuous 1's in an array or you can use binary string for convenience. Any solution using the binary string is acceptable.
My Solution is>>
#include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int main()
int n,q,k,count;
string str;
cin>>n>>q>>k;
bitset<MAX>s,c;
//inserting bits in array
for(int i=0;i<n;i++)
int temp;
cin>>temp;
s[i]=temp;
cin>>str;
for(int i=0;i<q;i++)
//making duplicate bitset
c=s;
if(str[i]=='?')
count=0;
while(c!=0)
//using bitmask to count maximum no of continuous 1's-O(1's bit)
c=(c&(c<<1));
count++;
if(count>k)
cout<<k<<"n";
else
cout<<count<<"n";
else
//shifting each bit to right and updating first bit with previous last
// bit
bool lb=s[n-1];
s=s>>1;
s[n-1]=lb;
c++ programming-challenge binary-search bitset
c++ programming-challenge binary-search bitset
New contributor
New contributor
edited 1 hour ago
Stephen Rauch
3,75051530
3,75051530
New contributor
asked 3 hours ago
Raja Babu
61
61
New contributor
New contributor
1
Try to give an example with possible value and invalid inputs and expected outputs. it will help people to figure out the way and the purpose of your programme.
â Calak
1 hour ago
add a comment |Â
1
Try to give an example with possible value and invalid inputs and expected outputs. it will help people to figure out the way and the purpose of your programme.
â Calak
1 hour ago
1
1
Try to give an example with possible value and invalid inputs and expected outputs. it will help people to figure out the way and the purpose of your programme.
â Calak
1 hour ago
Try to give an example with possible value and invalid inputs and expected outputs. it will help people to figure out the way and the purpose of your programme.
â Calak
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
<bits/stdc++.h>
(like everything in your compiler's bits/
subtree) is not a standard header and therefore not portable. Even if you're willing to sacrifice portability, it's a poor choice, as it will slow compilation down compared to simply including what you use.
using namespace std;
is poor practice. It makes your code less clear, and it may even silently change its meaning.
Don't use the preprocessor to name constants. Use a properly scoped, strongly typed C++ constant:
constexpr std::size_t max = 100000;
When using input streams, always check that operations succeed before using their results.
Variable names should be more descriptive. I have no idea what n
, q
, and k
are supposed to be storing. In fact, these names are so useless that I gave up reading at this point - it's not at all clear what this is supposed to be doing.
sorry for the inconvenience caused.
â Raja Babu
1 hour ago
1
Don't be sorry - you asked for tips to improve your code, and I've offered some ideas that I think will improve it. That's exactly what we're here for!:-)
BTW, if you make changes, you might want to post a follow-up question for review - see the help centre for some advice of what to do or not do.
â Toby Speight
58 mins ago
add a comment |Â
up vote
1
down vote
Don't declare multiple variables on a single line. it is error prone and more difficult to read.
int n,q,k,count;
should be:
int n;
int q;
int k;
int count;
Not sure what I mean by error prone?
int* n,q,k,count;
How many pointers do you have? one. only n
would be a pointer in this declaration.
Let your operators breathe. The lack of whitespace makes your code harder to read.
for(int i = 0; i < n; i++)
int temp;
cin >> temp;
s[i] = temp;
this is a little easier to distinguish.
Prefer prefix to postfix
Use more consistent indentation. I had to read the code three times just to realize that the scope braces didn't line up with each other. I almost flagged to close because
for ()
else
would be broken, and that's how your braces line up horizontally.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
<bits/stdc++.h>
(like everything in your compiler's bits/
subtree) is not a standard header and therefore not portable. Even if you're willing to sacrifice portability, it's a poor choice, as it will slow compilation down compared to simply including what you use.
using namespace std;
is poor practice. It makes your code less clear, and it may even silently change its meaning.
Don't use the preprocessor to name constants. Use a properly scoped, strongly typed C++ constant:
constexpr std::size_t max = 100000;
When using input streams, always check that operations succeed before using their results.
Variable names should be more descriptive. I have no idea what n
, q
, and k
are supposed to be storing. In fact, these names are so useless that I gave up reading at this point - it's not at all clear what this is supposed to be doing.
sorry for the inconvenience caused.
â Raja Babu
1 hour ago
1
Don't be sorry - you asked for tips to improve your code, and I've offered some ideas that I think will improve it. That's exactly what we're here for!:-)
BTW, if you make changes, you might want to post a follow-up question for review - see the help centre for some advice of what to do or not do.
â Toby Speight
58 mins ago
add a comment |Â
up vote
3
down vote
<bits/stdc++.h>
(like everything in your compiler's bits/
subtree) is not a standard header and therefore not portable. Even if you're willing to sacrifice portability, it's a poor choice, as it will slow compilation down compared to simply including what you use.
using namespace std;
is poor practice. It makes your code less clear, and it may even silently change its meaning.
Don't use the preprocessor to name constants. Use a properly scoped, strongly typed C++ constant:
constexpr std::size_t max = 100000;
When using input streams, always check that operations succeed before using their results.
Variable names should be more descriptive. I have no idea what n
, q
, and k
are supposed to be storing. In fact, these names are so useless that I gave up reading at this point - it's not at all clear what this is supposed to be doing.
sorry for the inconvenience caused.
â Raja Babu
1 hour ago
1
Don't be sorry - you asked for tips to improve your code, and I've offered some ideas that I think will improve it. That's exactly what we're here for!:-)
BTW, if you make changes, you might want to post a follow-up question for review - see the help centre for some advice of what to do or not do.
â Toby Speight
58 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
<bits/stdc++.h>
(like everything in your compiler's bits/
subtree) is not a standard header and therefore not portable. Even if you're willing to sacrifice portability, it's a poor choice, as it will slow compilation down compared to simply including what you use.
using namespace std;
is poor practice. It makes your code less clear, and it may even silently change its meaning.
Don't use the preprocessor to name constants. Use a properly scoped, strongly typed C++ constant:
constexpr std::size_t max = 100000;
When using input streams, always check that operations succeed before using their results.
Variable names should be more descriptive. I have no idea what n
, q
, and k
are supposed to be storing. In fact, these names are so useless that I gave up reading at this point - it's not at all clear what this is supposed to be doing.
<bits/stdc++.h>
(like everything in your compiler's bits/
subtree) is not a standard header and therefore not portable. Even if you're willing to sacrifice portability, it's a poor choice, as it will slow compilation down compared to simply including what you use.
using namespace std;
is poor practice. It makes your code less clear, and it may even silently change its meaning.
Don't use the preprocessor to name constants. Use a properly scoped, strongly typed C++ constant:
constexpr std::size_t max = 100000;
When using input streams, always check that operations succeed before using their results.
Variable names should be more descriptive. I have no idea what n
, q
, and k
are supposed to be storing. In fact, these names are so useless that I gave up reading at this point - it's not at all clear what this is supposed to be doing.
answered 1 hour ago
Toby Speight
21.3k436104
21.3k436104
sorry for the inconvenience caused.
â Raja Babu
1 hour ago
1
Don't be sorry - you asked for tips to improve your code, and I've offered some ideas that I think will improve it. That's exactly what we're here for!:-)
BTW, if you make changes, you might want to post a follow-up question for review - see the help centre for some advice of what to do or not do.
â Toby Speight
58 mins ago
add a comment |Â
sorry for the inconvenience caused.
â Raja Babu
1 hour ago
1
Don't be sorry - you asked for tips to improve your code, and I've offered some ideas that I think will improve it. That's exactly what we're here for!:-)
BTW, if you make changes, you might want to post a follow-up question for review - see the help centre for some advice of what to do or not do.
â Toby Speight
58 mins ago
sorry for the inconvenience caused.
â Raja Babu
1 hour ago
sorry for the inconvenience caused.
â Raja Babu
1 hour ago
1
1
Don't be sorry - you asked for tips to improve your code, and I've offered some ideas that I think will improve it. That's exactly what we're here for!
:-)
BTW, if you make changes, you might want to post a follow-up question for review - see the help centre for some advice of what to do or not do.â Toby Speight
58 mins ago
Don't be sorry - you asked for tips to improve your code, and I've offered some ideas that I think will improve it. That's exactly what we're here for!
:-)
BTW, if you make changes, you might want to post a follow-up question for review - see the help centre for some advice of what to do or not do.â Toby Speight
58 mins ago
add a comment |Â
up vote
1
down vote
Don't declare multiple variables on a single line. it is error prone and more difficult to read.
int n,q,k,count;
should be:
int n;
int q;
int k;
int count;
Not sure what I mean by error prone?
int* n,q,k,count;
How many pointers do you have? one. only n
would be a pointer in this declaration.
Let your operators breathe. The lack of whitespace makes your code harder to read.
for(int i = 0; i < n; i++)
int temp;
cin >> temp;
s[i] = temp;
this is a little easier to distinguish.
Prefer prefix to postfix
Use more consistent indentation. I had to read the code three times just to realize that the scope braces didn't line up with each other. I almost flagged to close because
for ()
else
would be broken, and that's how your braces line up horizontally.
add a comment |Â
up vote
1
down vote
Don't declare multiple variables on a single line. it is error prone and more difficult to read.
int n,q,k,count;
should be:
int n;
int q;
int k;
int count;
Not sure what I mean by error prone?
int* n,q,k,count;
How many pointers do you have? one. only n
would be a pointer in this declaration.
Let your operators breathe. The lack of whitespace makes your code harder to read.
for(int i = 0; i < n; i++)
int temp;
cin >> temp;
s[i] = temp;
this is a little easier to distinguish.
Prefer prefix to postfix
Use more consistent indentation. I had to read the code three times just to realize that the scope braces didn't line up with each other. I almost flagged to close because
for ()
else
would be broken, and that's how your braces line up horizontally.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Don't declare multiple variables on a single line. it is error prone and more difficult to read.
int n,q,k,count;
should be:
int n;
int q;
int k;
int count;
Not sure what I mean by error prone?
int* n,q,k,count;
How many pointers do you have? one. only n
would be a pointer in this declaration.
Let your operators breathe. The lack of whitespace makes your code harder to read.
for(int i = 0; i < n; i++)
int temp;
cin >> temp;
s[i] = temp;
this is a little easier to distinguish.
Prefer prefix to postfix
Use more consistent indentation. I had to read the code three times just to realize that the scope braces didn't line up with each other. I almost flagged to close because
for ()
else
would be broken, and that's how your braces line up horizontally.
Don't declare multiple variables on a single line. it is error prone and more difficult to read.
int n,q,k,count;
should be:
int n;
int q;
int k;
int count;
Not sure what I mean by error prone?
int* n,q,k,count;
How many pointers do you have? one. only n
would be a pointer in this declaration.
Let your operators breathe. The lack of whitespace makes your code harder to read.
for(int i = 0; i < n; i++)
int temp;
cin >> temp;
s[i] = temp;
this is a little easier to distinguish.
Prefer prefix to postfix
Use more consistent indentation. I had to read the code three times just to realize that the scope braces didn't line up with each other. I almost flagged to close because
for ()
else
would be broken, and that's how your braces line up horizontally.
answered 42 mins ago
bruglesco
1,0871520
1,0871520
add a comment |Â
add a comment |Â
Raja Babu is a new contributor. Be nice, and check out our Code of Conduct.
Raja Babu is a new contributor. Be nice, and check out our Code of Conduct.
Raja Babu is a new contributor. Be nice, and check out our Code of Conduct.
Raja Babu is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207063%2fbitmasking-and-searching-consecutive-1s%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Try to give an example with possible value and invalid inputs and expected outputs. it will help people to figure out the way and the purpose of your programme.
â Calak
1 hour ago