Move Zeroes โ€“ Python (In-Place Solution)

๐Ÿšš Move Zeroes โ€“ Python (In-Place Solution)

Hi All,

Today I solved a popular problem: Move all zeroes to the end of the array while maintaining the order of non-zero elements.

๐Ÿ“Œ Problem Statement

Given an array nums, move all …


This content originally appeared on DEV Community and was authored by Manoj Kumar

๐Ÿšš Move Zeroes โ€“ Python (In-Place Solution)

Hi All,

Today I solved a popular problem: Move all zeroes to the end of the array while maintaining the order of non-zero elements.

๐Ÿ“Œ Problem Statement

Given an array nums, move all 0s to the end while:

  • Maintaining the relative order of non-zero elements
  • Performing the operation in-place (no extra array)

๐Ÿ” Examples

Example 1:

nums = [0, 1, 0, 3, 12]

Output:

[1, 3, 12, 0, 0]

Example 2:

nums = [0]

Output:

[0]

๐Ÿ’ก Approach

๐Ÿ”น Two Pointer Technique (Optimal)

๐Ÿ‘‰ Idea:

  • Use a pointer j to track position for non-zero elements
  • Traverse array with i
  • Swap non-zero elements forward

๐Ÿง  Step-by-Step Logic

  1. Initialize j = 0
  2. Traverse array:
    • If element is not zero:
      • Swap nums[i] with nums[j]
      • Increment j

๐Ÿ’ป Python Code

def moveZeroes(nums):
    j = 0

    for i in range(len(nums)):
        if nums[i] != 0:
            nums[i], nums[j] = nums[j], nums[i]
            j += 1

๐Ÿ” Dry Run

For:

nums = [0, 1, 0, 3, 12]

Steps:

  • Swap 1 โ†’ [1, 0, 0, 3, 12]
  • Swap 3 โ†’ [1, 3, 0, 0, 12]
  • Swap 12 โ†’ [1, 3, 12, 0, 0]

Final Output:

[1, 3, 12, 0, 0]

๐Ÿ–ฅ๏ธ Sample Output

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

Input: [0]
Output: [0]

โšก Complexity Analysis

  • Time Complexity: O(n) โœ…
  • Space Complexity: O(1) โœ…

๐Ÿง  Why this is important?

  • Uses two pointer technique
  • Maintains order (stable)
  • Common pattern in array problems

โœ… Conclusion

This problem helped me understand:

  • In-place array manipulation
  • Efficient element shifting
  • Two pointer strategy

๐Ÿš€ A must-know problem for beginners and interviews!


This content originally appeared on DEV Community and was authored by Manoj Kumar


Print Share Comment Cite Upload Translate Updates
APA

Manoj Kumar | Sciencx (2026-03-22T11:19:50+00:00) Move Zeroes โ€“ Python (In-Place Solution). Retrieved from https://www.scien.cx/2026/03/22/move-zeroes-python-in-place-solution-3/

MLA
" » Move Zeroes โ€“ Python (In-Place Solution)." Manoj Kumar | Sciencx - Sunday March 22, 2026, https://www.scien.cx/2026/03/22/move-zeroes-python-in-place-solution-3/
HARVARD
Manoj Kumar | Sciencx Sunday March 22, 2026 » Move Zeroes โ€“ Python (In-Place Solution)., viewed ,<https://www.scien.cx/2026/03/22/move-zeroes-python-in-place-solution-3/>
VANCOUVER
Manoj Kumar | Sciencx - » Move Zeroes โ€“ Python (In-Place Solution). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2026/03/22/move-zeroes-python-in-place-solution-3/
CHICAGO
" » Move Zeroes โ€“ Python (In-Place Solution)." Manoj Kumar | Sciencx - Accessed . https://www.scien.cx/2026/03/22/move-zeroes-python-in-place-solution-3/
IEEE
" » Move Zeroes โ€“ Python (In-Place Solution)." Manoj Kumar | Sciencx [Online]. Available: https://www.scien.cx/2026/03/22/move-zeroes-python-in-place-solution-3/. [Accessed: ]
rf:citation
» Move Zeroes โ€“ Python (In-Place Solution) | Manoj Kumar | Sciencx | https://www.scien.cx/2026/03/22/move-zeroes-python-in-place-solution-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.