2Brute2Best
πŸ”₯0

Move Zeroes

EasyΒ·LeetCode #283 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given an integer array nums.

Move every 0 to the end of the array, while the non-zero elements stay in the same relative order they started in.

Do it in place β€” change nums directly, without making a copy of the array. The function returns nothing.

Follow-up: can you keep the number of operations small?

Input

An integer array nums.

Output

Nothing is returned. nums itself must end with all non-zeros first (in original order) followed by all the zeros.

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

The non-zeros 1, 3, 12 keep their order and move to the front. The two zeros go to the end.

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

Only a zero β€” nothing to move.

Constraints
  • β€’ 1 ≀ nums.length ≀ 10^4
  • β€’ βˆ’2^31 ≀ nums[i] ≀ 2^31 βˆ’ 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œmove all 0s to the end”
β†’ Keep the non-zeros at the front β†’ filter with read / write, `val = 0`.
β€œkeep the relative order of the non-zero elements”
β†’ Write them forward one by one, never swap two non-zeros with each other.
β€œin place, without a copy”
β†’ Reuse the front of `nums`; swap so the zeros are not lost.
03

Approach Complexity Ladder

Brute Force β€” build a new arrayO(N)O(N)

Copy non-zeros into a new array, pad with zeros, copy back

Better β€” read / write, then fill zerosO(N)O(1)

Pack non-zeros to the front with read/write, then set the tail to 0

Optimal β€” read / write with swap (one pass)O(N)O(1)

Swap each non-zero into nums[write]; the zeros move back on their own

Brute Force Intuition

Make a new array. Append every non-zero in order, then append zeros until it has the same length as nums. Copy it back.

Recognition rule for this sub-pattern:

One pointer reads every element, the other marks where the next "kept" element should be written.
Remember this Β· Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.