Skip to main content

Minimum Swaps to Move Zeros to End - Solution & Explanation

EasyArrayTwo Pointers7 min read
Practice this problem

Problem Statement

You are given an integer array nums.

In one operation, you can choose any two distinct indices i and j and swap nums[i] and nums[j].

Return an integer denoting the minimum number of operations required to move all 0s to the end of the array.

 

Example 1:

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

Output: 2

Explanation:

We perform the following swap operations:

  • Swap nums[0] and nums[3], giving nums = [3, 1, 0, 0, 12].
  • Swap nums[2] and nums[4], giving nums = [3, 1, 12, 0, 0].

Thus, the answer is 2.

Example 2:

Input: nums = [0,1,0,2]

Output: 1

Explanation:

We perform the following swap operations:

  • Swap nums[0] and nums[3], giving nums = [2, 1, 0, 0].

Thus, the answer is 1.

Example 3:

Input: nums = [1,2,0]

Output: 0

Explanation:

The array already satisfies the condition. Therefore, no swap operations are needed.

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100

Approach Overview

Problem Overview: Given an array of integers, move every 0 to the end using swaps and return the minimum number of swaps required. The relative order of non‑zero elements does not matter for counting swaps, but each swap represents moving a zero past a non‑zero element.

Approach 1: Brute Force Adjacent Swapping (O(n2) time, O(1) space)

The straightforward idea is to repeatedly scan the array and swap a 0 with the next element whenever it appears before a non‑zero value. This effectively “bubbles” zeros toward the end, similar to bubble sort behavior. Each pass pushes zeros one step right until all zeros reach the tail of the array. The approach is easy to reason about but inefficient because each zero may move across many elements one swap at a time. This results in worst‑case quadratic time when many zeros appear near the start.

Approach 2: Two Pointers Simulation (O(n) time, O(1) space)

Use a two‑pointer technique where one pointer scans the array while another tracks the position of the next non‑zero element. When the scan pointer sees a non‑zero value after a zero region, swap it forward and increment a swap counter. This physically performs the rearrangement while ensuring zeros gradually accumulate at the end. Each element is processed once, so the complexity drops to linear time. This pattern is closely related to problems under two pointers and array manipulation.

Approach 3: Counting Non‑Zero Elements to the Right (Optimal) (O(n) time, O(1) space)

The minimum swap count can be computed without performing actual swaps. Traverse the array from right to left and maintain a counter of how many non‑zero elements have been seen. When you encounter a zero, it must cross every non‑zero element to its right to reach the end. Each crossing represents one swap, so add the current non‑zero count to the answer. When you encounter a non‑zero value, simply increment the counter. This works because the optimal sequence moves each zero across all trailing non‑zeros exactly once. The algorithm uses a single pass and constant memory, which is typical of efficient greedy counting strategies.

Recommended for interviews: Start by explaining the brute force swap simulation to demonstrate understanding of how zeros move through the array. Then move to the counting approach. Interviewers usually expect the linear scan solution because it shows you recognized that each zero must cross every non‑zero element to its right. That observation converts a simulation problem into a simple counting problem with O(n) time and O(1) space.

Solution

We use two pointers i and j pointing to the beginning and end of the array respectively. Each time, we move i to the right until we find a 0, and move j to the left until we find a non-zero number. If i < j, we swap the two elements and increment the answer by 1. We repeat this process until i geq j.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Adjacent SwapsO(n^2)O(1)Conceptual understanding or very small arrays
Two Pointers SimulationO(n)O(1)When you also need to physically rearrange the array
Right-to-Left Counting (Optimal)O(n)O(1)Best for computing the minimum swap count without modifying the array

Video Solution

3936. Minimum Swaps to Move Zeros to End (Leetcode Easy)Programming Live with Larry170 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Minimum Swaps to Move Zeros to End easy or hard?
The problem is typically classified as Easy because the optimal solution requires a single pass and simple counting logic. The main challenge is recognizing that each zero must swap with every non‑zero element to its right, which converts the problem into a straightforward counting task.
Minimum Swaps to Move Zeros to End Python/Java solution
Both Python and Java implementations follow the same linear scan logic. Iterate from the end of the array, maintain a count of non‑zero values, and add that count whenever a zero appears. The algorithm runs in O(n) time and constant space in either language.
How to solve Minimum Swaps to Move Zeros to End in O(n)?
Traverse the array from right to left and track how many non‑zero values have been seen so far. When encountering a zero, add the current non‑zero count to the swap total because the zero must cross each of those elements. When encountering a non‑zero value, increment the counter. This single pass computes the exact number of required swaps.
What is the best approach for Minimum Swaps to Move Zeros to End?
The optimal approach scans the array from right to left while counting how many non‑zero elements have appeared. Every time a zero is found, it must cross all those non‑zero elements to reach the end, so the swap count increases by that number. This method runs in O(n) time and uses O(1) extra space.
Is Minimum Swaps to Move Zeros to End asked at Google/Amazon/Meta?
Variants of moving zeros or counting swaps in arrays appear frequently in coding interviews at companies like Amazon, Google, and Meta. The exact wording may differ, but the core idea—counting inversions or using two‑pointer array techniques—is common in interview question banks.
What data structure is used in Minimum Swaps to Move Zeros to End?
The problem primarily uses arrays with simple counters or two pointers. No advanced data structures are required. The key idea is recognizing the relationship between zeros and the non‑zero elements that appear after them.
What is the time complexity of Minimum Swaps to Move Zeros to End?
The optimal solution runs in O(n) time because the array is scanned once while maintaining a counter of non‑zero elements. Space complexity is O(1) since only a few integer variables are used regardless of input size.

Ready to solve this problem?

Practice Minimum Swaps to Move Zeros to End with our built-in code editor and test cases.

Practice on FleetCode