Minimum Swaps to Move Zeros to End - Solution & Explanation
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]andnums[3], givingnums = [3, 1, 0, 0, 12]. - Swap
nums[2]andnums[4], givingnums = [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]andnums[3], givingnums = [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 <= 1000 <= 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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Adjacent Swaps | O(n^2) | O(1) | Conceptual understanding or very small arrays |
| Two Pointers Simulation | O(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 Larry • 170 views views
Watch 1 more video solutions →Frequently Asked Questions
Is Minimum Swaps to Move Zeros to End easy or hard?
Minimum Swaps to Move Zeros to End Python/Java solution
How to solve Minimum Swaps to Move Zeros to End in O(n)?
What is the best approach for Minimum Swaps to Move Zeros to End?
Is Minimum Swaps to Move Zeros to End asked at Google/Amazon/Meta?
What data structure is used in Minimum Swaps to Move Zeros to End?
What is the time complexity of Minimum Swaps to Move Zeros to End?
Ready to solve this problem?
Practice Minimum Swaps to Move Zeros to End with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor