title: Duplicate zero in array by modifying the array in place
body: <blockquote>
  <p><em>There is a fixed length array <code>arr</code> of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. The elements beyond the length of the original array are not written.</em></p>
  </blockquote>
  <p>We have to modify input array in place and doesn't have to create new array.</p>
  <p>So I created that but it is duplicating the zero which is at the end of array and not the previous zeros. Can somebody help me with this?</p>
  <pre class="lang-java prettyprint-override"><code>public static void addPos() {
      int arr[] = { 1, 2, 0, 3, 0, 5, 0, 7, 8 };
      int result[] = new int[arr.length];
      for (int i = 0; i &lt; arr.length; i++) {
          if (arr[i] == 0) {
              int loc = i;
              for (int j = 0; j &lt; loc; j++) {
                  result[j] = arr[j];
                  result[loc] = 0;
              }
              for (int j = loc + 1; j &lt; arr.length; j++) {
                  result[j] = arr[j - 1];
              }
          }
      }
      for (int k = 0; k &lt; arr.length; k++)
          System.out.println(result[k]);
  }
  </code></pre>
  <h2>Output</h2>
  <pre><code>1
  2
  0
  3
  0
  5
  0
  0
  7
  </code></pre>
  <pre><code>Expected output:
  1
  2
  0
  0
  3
  0
  0
  5
  0
  </code></pre>