Bitmasking and searching consecutive 1's 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&lt;bits/stdc++.h&gt;
using namespace std;
#define MAX 100000
int main()
{
  int n,q,k,count;
  string str;
  cin&gt;&gt;n&gt;&gt;q&gt;&gt;k;
  bitset&lt;MAX&gt;s,c;
  //inserting bits in array
  for(int i=0;i&lt;n;i++)
  {
    int temp;
    cin&gt;&gt;temp;
    s[i]=temp;
   }
   cin&gt;&gt;str;
  for(int i=0;i&lt;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&amp;(c&lt;&lt;1)); 
            count++; 
          }
      if(count&gt;k)
         cout&lt;&lt;k&lt;&lt;"\n";
      else
         cout&lt;&lt;count&lt;&lt;"\n";
   }
   else
   {
       //shifting each bit to right and updating first bit with previous last
      // bit
       bool lb=s[n-1];
       s=s&gt;&gt;1;
       s[n-1]=lb;
    }
  }
}
&lt;bits/stdc++.h&gt; (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.