NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 26. Remove Duplicates from Sorted ArrayEasy
  • 27. Remove ElementEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 49. Group AnagramsMedium

283. Move Zeroes

Easy
ArrayTwo Pointers
Companies:
Facebook
Answered: Feb 24, 2026
View on LeetCode

Problem Description

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Examples

Example 1

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]

Example 2

Input: nums = [0]
Output: [0]

Constraints

  • 1 <= nums.length <= 104
  • -231 <= nums[i] <= 231 - 1

Follow-up

Could you minimize the total number of operations done?

💡 Hints (2)


Solutions

Complexity Analysis

Time Complexity:O(n2)

In the worst case, for each zero element, we may have to scan the remaining array to find a non-zero element.

Space Complexity:O(1)

We are modifying the array in-place and not using any extra space.

The brute force approach iterates through the array and whenever it encounters a zero, it looks for the next non-zero element and swaps it with the current zero element. This process continues until all zeros are moved to the end of the array.

Complexity Analysis

Time Complexity:O(n)

We iterate through the array once, and each swap operation takes constant time.

Space Complexity:O(1)

We are modifying the array in-place and not using any extra space.

The two-pointer approach uses two pointers, left and right. The right pointer iterates through the array, while the left pointer keeps track of the position where the next non-zero element should be placed. Whenever a non-zero element is found at the right pointer, it is swapped with the element at the left pointer, and the left pointer is incremented. This way, all non-zero elements are moved to the front of the array, and all zeros are moved to the end.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Brute Force
O(n2)O(n^{2})O(n2)O(1)O(1)O(1)Simple to understand⚠️ Inefficient — nested loops for each zero
Two Pointers
O(n)O(n)O(n)O(1)O(1)O(1)Optimal time and space, maintains relative order, single passNone

The optimal approach for this problem is Solution 2: Two Pointers, which achieves O(n)O(n)O(n) time complexity and O(1)O(1)O(1) space complexity. It uses a left pointer to track where the next non-zero element should be placed, and a right pointer to scan through the array, swapping non-zero elements into position in a single pass.


Previous242. Valid Anagram
Next344. Reverse String
Related posts
  • 26. Remove Duplicates from Sorted ArrayEasy
  • 27. Remove ElementEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 49. Group AnagramsMedium
On this page
14 lines
1class Solution:
2 def moveZeroes(self, nums: List[int]) -> None:
3 """
4 Do not return anything, modify nums in-place instead.
5 """
6 for i in range(len(nums)):
7 if nums[i] == 0:
8 # Find the next non-zero element and swap it with the current zero element
9 for j in range(i, len(nums)):
10 if nums[j] != 0:
11 temp = nums[j]
12 nums[j] = nums[i]
13 nums[i] = temp
14 break
13 lines
1class Solution:
2 def moveZeroes(self, nums: List[int]) -> None:
3 """
4 Do not return anything, modify nums in-place instead.
5 """
6 left = 0
7
8 for right in range(len(nums)):
9 # If the current element is not zero, swap it with the element at the left pointer
10 if nums[right] != 0:
11 nums[left], nums[right] = nums[right], nums[left]
12
13 left += 1