> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2023/10-2023-oct-30/31-move-all-zeroes-to-the-end-of-the-array.md).

# 31. Move all zeroes to the end of the array

The problem can be found at the following link: [Question Link](https://practice.geeksforgeeks.org/problems/move-all-zeroes-to-end-of-array0751/1)

![](https://badgen.net/badge/Level/Easy/green)

## My Approach

To move all zeroes to the end of the array, I use a two-pointer approach.

* I maintain two pointers, `i` and `j`.
* Pointer `i` is used to iterate through the array and move non-zero elements to the front of the array.
* Pointer `j` keeps track of the position where we should place the next non-zero element.
* Once we have moved all non-zero elements to the front, I fill the remaining positions with zeroes.

## Time and Auxiliary Space Complexity

* **Time Complexity**: `O(n)`, where `n` is the number of elements in the array.
* **Auxiliary Space Complexity**: `O(1)`, as we are not using any extra data structures.

## Code (C++)

```cpp
class Solution {
public:
    void pushZerosToEnd(int arr[], int n) {
        int i = 0;
        for (int j = 0; j < n; ++j) {
            if (arr[j]) {
                arr[i++] = arr[j];
            }
        }

        for (; i < n; ++i) {
            arr[i] = 0;
        }
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# 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:

```
GET https://gl01.gitbook.io/gfg-editorials/2023/10-2023-oct-30/31-move-all-zeroes-to-the-end-of-the-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
