Skip to content

Commit 310aa20

Browse files
committed
Move Zeroes
1 parent 67de492 commit 310aa20

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 283. Move Zeroes
2+
3+
**Difficulty:** *Easy*
4+
**Category:** *Arrays, Two Pointers*
5+
**Leetcode Link:** [Problem Link](https://leetcode.com/problems/move-zeroes/)
6+
7+
---
8+
9+
## 📝 Introduction
10+
11+
*Given an array, the task is to move all the zeros to the end of the array while maintaining the relative order of the non-zero elements.*
12+
13+
*Constraints typically include:<br>
14+
- The operation must be done in-place without making a copy of the array.<br>
15+
- Minimize the total number of operations.*
16+
17+
---
18+
19+
## 💡 Approach & Key Insights
20+
21+
*The key idea is to segregate non-zero elements from zeros without disturbing their relative order. There are multiple approaches:<br>
22+
- Brute force copies non-zero elements to a temporary array.<br>
23+
- Optimal approach uses two pointers to do in-place swapping, reducing space complexity and preserving order efficiently.*
24+
25+
---
26+
27+
## 🛠️ Breakdown of Approaches
28+
29+
### 1️⃣ Brute Force / Naive Approach
30+
31+
- **Explanation:** *We copy all non-zero elements to a temporary array. Then, we place them back into the original array starting from the beginning. Finally, we fill the remaining positions with zeros.*
32+
- **Time Complexity:** *O(n) – linear scan to collect non-zeros and overwrite.*
33+
- **Space Complexity:** *O(n) – extra space for temporary array.*
34+
- **Example/Dry Run:**
35+
36+
```plaintext
37+
Input: [1, 0, 2, 3, 2, 0, 0, 4, 5, 1]
38+
Step 1: temp = [1, 2, 3, 2, 4, 5, 1]
39+
Step 2: Overwrite first 7 positions with temp → [1, 2, 3, 2, 4, 5, 1, _, _, _]
40+
Step 3: Fill remaining with 0 → [1, 2, 3, 2, 4, 5, 1, 0, 0, 0]
41+
```
42+
43+
### 2️⃣ Optimized Approach
44+
45+
- **Explanation:** *Use two pointers `j` and `i`. Pointer `j` finds the first zero, and `i` scans ahead. When `a[i]` is non-zero, swap `a[i]` with `a[j]` and move `j` forward.*
46+
- **Time Complexity:** *O(n) – each element is visited once.*
47+
- **Space Complexity:** *O(1) – in-place operation.*
48+
- **Example/Dry Run:**
49+
50+
```plaintext
51+
Input: [1, 0, 2, 3, 2, 0, 0, 4, 5, 1]
52+
Step 1: j = index of first 0 → j = 1
53+
Step 2: i = j + 1 = 2
54+
Iterate:
55+
i=2 → a[2]=2 ≠ 0 → swap a[2] and a[1] → [1, 2, 0, 3, 2, 0, 0, 4, 5, 1], j=2
56+
i=3 → a[3]=3 ≠ 0 → swap a[3] and a[2] → [1, 2, 3, 0, 2, 0, 0, 4, 5, 1], j=3
57+
i=4 → a[4]=2 ≠ 0 → swap a[4] and a[3] → [1, 2, 3, 2, 0, 0, 0, 4, 5, 1], j=4
58+
... and so on.
59+
Final Output: [1, 2, 3, 2, 4, 5, 1, 0, 0, 0]
60+
```
61+
62+
---
63+
64+
## 📊 Complexity Analysis
65+
66+
| Approach | Time Complexity | Space Complexity |
67+
| ------------- | --------------- | ---------------- |
68+
| Brute Force | O(n) | O(n) |
69+
| Optimized | O(n) | O(1) |
70+
71+
---
72+
73+
## 📉 Optimization Ideas
74+
75+
*The two-pointer method is already optimal in terms of both time and space. Further optimizations are unnecessary, though one could explore variations like minimizing swaps.*
76+
77+
---
78+
79+
## 📌 Example Walkthroughs & Dry Runs
80+
81+
```plaintext
82+
Example:
83+
Input: [0, 1, 0, 3, 12]
84+
Two pointers:
85+
j = 0 (points to 0)
86+
i = 1 → a[1]=1 ≠ 0 → swap → [1, 0, 0, 3, 12], j=1
87+
i = 2 → a[2]=0 → skip
88+
i = 3 → a[3]=3 ≠ 0 → swap → [1, 3, 0, 0, 12], j=2
89+
i = 4 → a[4]=12 ≠ 0 → swap → [1, 3, 12, 0, 0], j=3
90+
Output: [1, 3, 12, 0, 0]
91+
```
92+
93+
---
94+
95+
## 🔗 Additional Resources
96+
97+
- [Python list methods](https://docs.python.org/3/tutorial/datastructures.html)
98+
99+
---
100+
101+
Author: Neha Amin <br>
102+
Date: 18/07/2025
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def moveZeroes(self, nums: List[int]) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
j = -1
7+
n = len(nums)
8+
9+
# Place the pointer j at the first zero
10+
for i in range(n):
11+
if nums[i] == 0:
12+
j = i
13+
break
14+
15+
# No zeros found, return early
16+
if j == -1:
17+
return
18+
19+
# Move the pointers i and j and swap accordingly
20+
for i in range(j + 1, n):
21+
if nums[i] != 0:
22+
nums[i], nums[j] = nums[j], nums[i]
23+
j += 1

0 commit comments

Comments
 (0)