title: C++ Replace even number of zeros in an array with only two zeros
body: <p>My program should replace the group with an even number of zeros, which go in a row, with two zeros and group with an odd number of zeros replace with only one zero. If the input has no zeroes, it should be left as is. I've written following code to solve this problem.</p>
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;vector&gt; 
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  int main() {
      int n;
      cout &lt;&lt; &quot;Enter an array size:\n&quot;;
      cout &lt;&lt; &quot;n = &quot;;
      cin &gt;&gt; n;
      vector&lt;int&gt; arr(n);
      vector&lt;int&gt; temp_arr(n);
  
      cout &lt;&lt; &quot;Enter an array:\n&quot;;
      for (int i = 0; i &lt; n; i++) {
          cin &gt;&gt; arr[i];
      }
  
      for (int i = arr.size() - 1; i &gt;= 1; i--) {
          if (arr[i] == arr[i - 1] &amp;&amp; arr[i] == 0){
              temp_arr.push_back(i);
          }
          else if (arr[i] != 0) {
              if (temp_arr.size() % 2 == 1) {
                  arr.erase(arr.begin() + i);
                  temp_arr.clear();
              }
              else if (temp_arr.size() % 2 == 0){
                  arr.erase(arr.begin() + i);
                  arr.insert(arr.begin() + i , 0);
                  temp_arr.clear();
              }
          }
      }
      cout &lt;&lt; &quot;Output of the program:\n&quot;;
  
      for (int i = 0; i &lt; arr.size(); i++) {
          cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;
      }
      system(&quot;pause&quot;);
      return 0;
  }
  </code></pre>
  <p>But after compilation I've got this:</p>
  <p><img src="https://i.stack.imgur.com/8XpJO.jpg" alt="The whole array was replaced with zeros" /></p>
  <p>The whole array was replaced with zeros.</p>
  <p>What did I do wrong?</p>