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

# Duplicate Zeros

## [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros)

Given a fixed length array `arr` of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array **in place**, do not return anything from your function.

**Example 1:**

```

Input: [1,0,2,3,0,4,5,0]
Output: null
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
```

**Example 2:**

```

Input: [1,2,3]
Output: null
Explanation: After calling your function, the input array is modified to: [1,2,3]
```

**Note:**

1. `1 <= arr.length <= 10000`
2. `0 <= arr[i] <= 9`

## Solutions

### 🧠 Cpp

```cpp
class Solution
{
    template<typename T>
    void insert_stable_size(size_t pos, T val, vector<T> &arr)
    {
        for(size_t i = pos; i < arr.size(); ++i)
        {
            std::swap(val, arr[i]);
        }
    }
public:
    void duplicateZeros(vector<int>& arr)
    {
        //I am not using insert + pop_back
        //because the goal if the task is to do it manually
        for(size_t i = 0; i < arr.size()-1; ++i)
        {
            if(arr[i] == 0)
            {
                insert_stable_size(i+1, 0, arr);
                //skip inserted 0
                ++i;
            }
        }
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://anton-veselskyi.gitbook.io/codding-problems-solutions/leetcode/easy/duplicate-zeros.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
