Bitmasking and searching consecutive 1's

The name of the pictureThe name of the pictureThe name of the pictureClash 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;











share









New contributor




Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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














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;











share









New contributor




Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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












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;











share









New contributor




Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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





share









New contributor




Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share









New contributor




Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share



share








edited 1 hour ago









Stephen Rauch

3,75051530




3,75051530






New contributor




Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Raja Babu

61




61




New contributor




Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Raja Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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




    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










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.






share|improve this answer




















  • 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


















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.






share|improve this answer




















    Your Answer





    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Raja Babu is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer




















    • 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















    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.






    share|improve this answer




















    • 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













    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.






    share|improve this answer












    <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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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













    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 42 mins ago









        bruglesco

        1,0871520




        1,0871520




















            Raja Babu is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke