Move Zeroes
Given an integer array nums, move all 0s to the end of the array while maintaining the relative order of the non-zero elements.
You must modify the array in-place.
Example 1
Input
n = 5 nums = [0, 1, 0, 3, 12]
Output
[1, 3, 12, 0, 0]
Explanation
All non-zero elements remain in their original relative order, while the zeroes are moved to the end.
Example 2
Input
n = 5 nums = [0, 0, 1, 2, 3]
Output
[1, 2, 3, 0, 0]
Explanation
The three non-zero elements are kept in the same order and both zeroes are moved to the end.
Constraints
Hints:
Hint 1
Use a pointer to track the position where the next non-zero element should be placed.
Hint 2
Traverse the array and move every non-zero element toward the front.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 27, 2026
Last updated
Aug 27, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output