> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/06-03-2022-duplicate-zeros.md).

# 06/03/2022   Duplicate Zeros

Method1:

use an arraylist to copy the value.&#x20;

Time O(N) Space O(N)

Method2:

use queue. in the for loop, add each value into queue, if current value is 0, we add two times. then set arr\[i] = poped value from queue.

Time O(N)

Space O(N)

Method 3:

Time O(n)

Space O(1)

```
// Some code
class Solution {
    public void duplicateZeros(int[] arr) {
        int count = 0;
       for (int n : arr)
           if (n == 0)
               count++;
        if (count == 0) return;
        for (int i = arr.length - 1; i>= 0; i--){
            int temp = i + count;
            if (temp < arr.length){
                arr[temp] = arr[i];
            }
            //include case that temp > arr.length and not
            if (arr[i] == 0){
                count--;
                temp = i + count;
                if (temp < arr.length){
                    arr[temp] = arr[i];
                }
            }
            
        }
    }
}
```
