
Move Zeroes (easy)
Problem Statement
Given an array of integers nums, move all the 0s, which are present in the array to the end while maintaining the relative order of the non-zero elements.
Note: This rearrangement should be done in-place without using extra space for another array.
Example 1:
- Input:
[1, 0, 2, 0, 3, 0, 4] - Expected Output:
[1, 2, 3, 4, 0, 0, 0] - Justification: Here, all non-zero elements (1, 2, 3, 4) retain their order, and all zeros are moved to the end of the array.
Example 2:
- Input:
[0, 0, 0, 10, 20] - Expected Output:
[10, 20, 0, 0, 0] - Justification: The non-zero elements (10, 20) are shifted to the front, and the zeros are relocated to the end.
Example 3:
- Input:
[5, 1, 0, 2, 0] - Expected Output:
[5, 1, 2, 0, 0] - Justification: Non-zero elements (5, 1, 2) maintain their sequence, while zeros are moved to the end.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory