Bitmasking and searching consecutive 1's











up vote
4
down vote

favorite












I have written code to count the continuous ones 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|improve this question









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.
















  • 4




    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
    Nov 6 at 14:52















up vote
4
down vote

favorite












I have written code to count the continuous ones 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|improve this question









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.
















  • 4




    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
    Nov 6 at 14:52













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have written code to count the continuous ones 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|improve this question









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 ones 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 bitset






share|improve this question









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|improve this question









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|improve this question




share|improve this question








edited Nov 6 at 16:26









bruglesco

1,1092520




1,1092520






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 Nov 6 at 13:23









Raja Babu

274




274




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.








  • 4




    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
    Nov 6 at 14:52














  • 4




    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
    Nov 6 at 14:52








4




4




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
Nov 6 at 14:52




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
Nov 6 at 14:52










3 Answers
3






active

oldest

votes

















up vote
10
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





















  • i don't know how to do it.
    – Raja Babu
    Nov 6 at 16:26


















up vote
5
down vote














  • When you post code to be reviewed:


    • If your code ask for input, provide a set of valid an invalids inputs

    • If your code output a result, express expected.



  • Since you present your code, it may be interesting to embrace a coding standard. You do not pay taxes on written characters. Do not be afraid to use spaces to improve readability.


  • Don't include <bits/stdc++.h>`:


    • It's not a standard header. If you try to compile your code with MSVC, he will complain that he can't find him.

    • Even if you use only GCC, it would include so much unnecessary things that your compilation time will increase considerably.

    • Instead, explicitly includes all header needed.




  • Don't use using namespace std; :


    • Although it's "safe" to use it in some place (e.g. implementation files), as long as you are not familiar with the features of c ++, try to avoid it.

    • It led to a world of name collisions. (best case)

    • It's source of silent errors and weird bugs. (worst case)

    • If typing std:: is so tedious for you, try to import only truncated namespaces. ( eg using namespace std::string;).

    • If importing nested namespaces still too awful for you, try to do it inside a restricted scope (eg a functions) and not in global scope.




  • Don't use preprocessors to defines constants values, instead use const variable.


  • Chose good name for variables. what's n, q or k mean? They are int, but? And for s or c, we know they are bitset<MAX>, but nothing more.


  • Write expressive code, in which your intentions are clearly stated.


  • Don't declare more than one variable at a time. It led to error, mainly working with pointer or initialization.


  • Always initialize a variable when you declare it.

  • Extract the code into reusable functions , short as possible, and that operate a reduced and well defined number of statements. (see Single responsibility principle). (eg, a function that asks the user to enter an input, reads them and returns them)

  • If you ask user to input some values, really ask (tell to user what he have to write)

  • When you read values from user input, consider it ill-formed, so check for validity. Here you don't check after getting the int's. Futhermore, you don't check the length of the string and but access it by indexes, etc.

  • The main function should return an integer.






share|improve this answer






























    up vote
    3
    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





















    • FWIW, if * for pointer types are written before the variable name, then the declaration becomes int *n, q, k, count, which is less ambiguous.
      – Quelklef
      2 days ago








    • 2




      @Quelklef yes but that is also often taught as a poor practice in C++. Regardless single line multi-variable declarations are harder to read.
      – bruglesco
      2 days ago











    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
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    10
    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





















    • i don't know how to do it.
      – Raja Babu
      Nov 6 at 16:26















    up vote
    10
    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





















    • i don't know how to do it.
      – Raja Babu
      Nov 6 at 16:26













    up vote
    10
    down vote










    up vote
    10
    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 Nov 6 at 14:28









    Toby Speight

    21.6k536106




    21.6k536106












    • i don't know how to do it.
      – Raja Babu
      Nov 6 at 16:26


















    • i don't know how to do it.
      – Raja Babu
      Nov 6 at 16:26
















    i don't know how to do it.
    – Raja Babu
    Nov 6 at 16:26




    i don't know how to do it.
    – Raja Babu
    Nov 6 at 16:26












    up vote
    5
    down vote














    • When you post code to be reviewed:


      • If your code ask for input, provide a set of valid an invalids inputs

      • If your code output a result, express expected.



    • Since you present your code, it may be interesting to embrace a coding standard. You do not pay taxes on written characters. Do not be afraid to use spaces to improve readability.


    • Don't include <bits/stdc++.h>`:


      • It's not a standard header. If you try to compile your code with MSVC, he will complain that he can't find him.

      • Even if you use only GCC, it would include so much unnecessary things that your compilation time will increase considerably.

      • Instead, explicitly includes all header needed.




    • Don't use using namespace std; :


      • Although it's "safe" to use it in some place (e.g. implementation files), as long as you are not familiar with the features of c ++, try to avoid it.

      • It led to a world of name collisions. (best case)

      • It's source of silent errors and weird bugs. (worst case)

      • If typing std:: is so tedious for you, try to import only truncated namespaces. ( eg using namespace std::string;).

      • If importing nested namespaces still too awful for you, try to do it inside a restricted scope (eg a functions) and not in global scope.




    • Don't use preprocessors to defines constants values, instead use const variable.


    • Chose good name for variables. what's n, q or k mean? They are int, but? And for s or c, we know they are bitset<MAX>, but nothing more.


    • Write expressive code, in which your intentions are clearly stated.


    • Don't declare more than one variable at a time. It led to error, mainly working with pointer or initialization.


    • Always initialize a variable when you declare it.

    • Extract the code into reusable functions , short as possible, and that operate a reduced and well defined number of statements. (see Single responsibility principle). (eg, a function that asks the user to enter an input, reads them and returns them)

    • If you ask user to input some values, really ask (tell to user what he have to write)

    • When you read values from user input, consider it ill-formed, so check for validity. Here you don't check after getting the int's. Futhermore, you don't check the length of the string and but access it by indexes, etc.

    • The main function should return an integer.






    share|improve this answer



























      up vote
      5
      down vote














      • When you post code to be reviewed:


        • If your code ask for input, provide a set of valid an invalids inputs

        • If your code output a result, express expected.



      • Since you present your code, it may be interesting to embrace a coding standard. You do not pay taxes on written characters. Do not be afraid to use spaces to improve readability.


      • Don't include <bits/stdc++.h>`:


        • It's not a standard header. If you try to compile your code with MSVC, he will complain that he can't find him.

        • Even if you use only GCC, it would include so much unnecessary things that your compilation time will increase considerably.

        • Instead, explicitly includes all header needed.




      • Don't use using namespace std; :


        • Although it's "safe" to use it in some place (e.g. implementation files), as long as you are not familiar with the features of c ++, try to avoid it.

        • It led to a world of name collisions. (best case)

        • It's source of silent errors and weird bugs. (worst case)

        • If typing std:: is so tedious for you, try to import only truncated namespaces. ( eg using namespace std::string;).

        • If importing nested namespaces still too awful for you, try to do it inside a restricted scope (eg a functions) and not in global scope.




      • Don't use preprocessors to defines constants values, instead use const variable.


      • Chose good name for variables. what's n, q or k mean? They are int, but? And for s or c, we know they are bitset<MAX>, but nothing more.


      • Write expressive code, in which your intentions are clearly stated.


      • Don't declare more than one variable at a time. It led to error, mainly working with pointer or initialization.


      • Always initialize a variable when you declare it.

      • Extract the code into reusable functions , short as possible, and that operate a reduced and well defined number of statements. (see Single responsibility principle). (eg, a function that asks the user to enter an input, reads them and returns them)

      • If you ask user to input some values, really ask (tell to user what he have to write)

      • When you read values from user input, consider it ill-formed, so check for validity. Here you don't check after getting the int's. Futhermore, you don't check the length of the string and but access it by indexes, etc.

      • The main function should return an integer.






      share|improve this answer

























        up vote
        5
        down vote










        up vote
        5
        down vote










        • When you post code to be reviewed:


          • If your code ask for input, provide a set of valid an invalids inputs

          • If your code output a result, express expected.



        • Since you present your code, it may be interesting to embrace a coding standard. You do not pay taxes on written characters. Do not be afraid to use spaces to improve readability.


        • Don't include <bits/stdc++.h>`:


          • It's not a standard header. If you try to compile your code with MSVC, he will complain that he can't find him.

          • Even if you use only GCC, it would include so much unnecessary things that your compilation time will increase considerably.

          • Instead, explicitly includes all header needed.




        • Don't use using namespace std; :


          • Although it's "safe" to use it in some place (e.g. implementation files), as long as you are not familiar with the features of c ++, try to avoid it.

          • It led to a world of name collisions. (best case)

          • It's source of silent errors and weird bugs. (worst case)

          • If typing std:: is so tedious for you, try to import only truncated namespaces. ( eg using namespace std::string;).

          • If importing nested namespaces still too awful for you, try to do it inside a restricted scope (eg a functions) and not in global scope.




        • Don't use preprocessors to defines constants values, instead use const variable.


        • Chose good name for variables. what's n, q or k mean? They are int, but? And for s or c, we know they are bitset<MAX>, but nothing more.


        • Write expressive code, in which your intentions are clearly stated.


        • Don't declare more than one variable at a time. It led to error, mainly working with pointer or initialization.


        • Always initialize a variable when you declare it.

        • Extract the code into reusable functions , short as possible, and that operate a reduced and well defined number of statements. (see Single responsibility principle). (eg, a function that asks the user to enter an input, reads them and returns them)

        • If you ask user to input some values, really ask (tell to user what he have to write)

        • When you read values from user input, consider it ill-formed, so check for validity. Here you don't check after getting the int's. Futhermore, you don't check the length of the string and but access it by indexes, etc.

        • The main function should return an integer.






        share|improve this answer















        • When you post code to be reviewed:


          • If your code ask for input, provide a set of valid an invalids inputs

          • If your code output a result, express expected.



        • Since you present your code, it may be interesting to embrace a coding standard. You do not pay taxes on written characters. Do not be afraid to use spaces to improve readability.


        • Don't include <bits/stdc++.h>`:


          • It's not a standard header. If you try to compile your code with MSVC, he will complain that he can't find him.

          • Even if you use only GCC, it would include so much unnecessary things that your compilation time will increase considerably.

          • Instead, explicitly includes all header needed.




        • Don't use using namespace std; :


          • Although it's "safe" to use it in some place (e.g. implementation files), as long as you are not familiar with the features of c ++, try to avoid it.

          • It led to a world of name collisions. (best case)

          • It's source of silent errors and weird bugs. (worst case)

          • If typing std:: is so tedious for you, try to import only truncated namespaces. ( eg using namespace std::string;).

          • If importing nested namespaces still too awful for you, try to do it inside a restricted scope (eg a functions) and not in global scope.




        • Don't use preprocessors to defines constants values, instead use const variable.


        • Chose good name for variables. what's n, q or k mean? They are int, but? And for s or c, we know they are bitset<MAX>, but nothing more.


        • Write expressive code, in which your intentions are clearly stated.


        • Don't declare more than one variable at a time. It led to error, mainly working with pointer or initialization.


        • Always initialize a variable when you declare it.

        • Extract the code into reusable functions , short as possible, and that operate a reduced and well defined number of statements. (see Single responsibility principle). (eg, a function that asks the user to enter an input, reads them and returns them)

        • If you ask user to input some values, really ask (tell to user what he have to write)

        • When you read values from user input, consider it ill-formed, so check for validity. Here you don't check after getting the int's. Futhermore, you don't check the length of the string and but access it by indexes, etc.

        • The main function should return an integer.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered Nov 6 at 20:06









        Calak

        1,15312




        1,15312






















            up vote
            3
            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





















            • FWIW, if * for pointer types are written before the variable name, then the declaration becomes int *n, q, k, count, which is less ambiguous.
              – Quelklef
              2 days ago








            • 2




              @Quelklef yes but that is also often taught as a poor practice in C++. Regardless single line multi-variable declarations are harder to read.
              – bruglesco
              2 days ago















            up vote
            3
            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





















            • FWIW, if * for pointer types are written before the variable name, then the declaration becomes int *n, q, k, count, which is less ambiguous.
              – Quelklef
              2 days ago








            • 2




              @Quelklef yes but that is also often taught as a poor practice in C++. Regardless single line multi-variable declarations are harder to read.
              – bruglesco
              2 days ago













            up vote
            3
            down vote










            up vote
            3
            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 Nov 6 at 15:43









            bruglesco

            1,1092520




            1,1092520












            • FWIW, if * for pointer types are written before the variable name, then the declaration becomes int *n, q, k, count, which is less ambiguous.
              – Quelklef
              2 days ago








            • 2




              @Quelklef yes but that is also often taught as a poor practice in C++. Regardless single line multi-variable declarations are harder to read.
              – bruglesco
              2 days ago


















            • FWIW, if * for pointer types are written before the variable name, then the declaration becomes int *n, q, k, count, which is less ambiguous.
              – Quelklef
              2 days ago








            • 2




              @Quelklef yes but that is also often taught as a poor practice in C++. Regardless single line multi-variable declarations are harder to read.
              – bruglesco
              2 days ago
















            FWIW, if * for pointer types are written before the variable name, then the declaration becomes int *n, q, k, count, which is less ambiguous.
            – Quelklef
            2 days ago






            FWIW, if * for pointer types are written before the variable name, then the declaration becomes int *n, q, k, count, which is less ambiguous.
            – Quelklef
            2 days ago






            2




            2




            @Quelklef yes but that is also often taught as a poor practice in C++. Regardless single line multi-variable declarations are harder to read.
            – bruglesco
            2 days ago




            @Quelklef yes but that is also often taught as a poor practice in C++. Regardless single line multi-variable declarations are harder to read.
            – bruglesco
            2 days ago










            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




















































































            Popular posts from this blog

            横浜市

            Neuseeland

            Hungria