Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Problem Statement
Given:
An array of integers.
Task:
Move all the 0s to the end of the array while maintaining the relative order
of non-zero elements.
Do this in-place without making a copy of the array.
🧾 Sample Input/Output
Input:
int[] arr = {0, 1, 0, 3, 12};
Output:
[1, 3, 12, 0, 0]
Input:
int[] arr = {1, 2, 0, 0, 4, 5};
Output:
[1, 2, 4, 5, 0, 0]
🔍 Naive Approach: Using
Extra Array
This violates the “in-place” constraint — but it’s easy to
start with.
public
static int[] moveZeroesCopy(int[] arr) {
int[] result = new int[arr.length];
int index = 0;
for (int num : arr) {
if (num != 0) {
result[index++] = num;
}
}
// zeros are already 0 by default in new
array
return result;
}
⛔ Space Complexity: O(n)
✅ Optimal In-Place Solution (Two
Pointers)
Use one pointer to track the current position to place
non-zero values.
public
static void moveZeroes(int[] arr) {
int index = 0;
// Step 1: Move all non-zero values forward
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
arr[index++] = arr[i];
}
}
// Step 2: Fill the rest with zeroes
while (index < arr.length) {
arr[index++] = 0;
}
}
🧠 Explanation
🔁 Real Interview
Follow-Up: One-Pass Swap
public
static void moveZeroesSwap(int[] arr) {
int lastNonZeroFoundAt = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
int temp = arr[lastNonZeroFoundAt];
arr[lastNonZeroFoundAt] = arr[i];
arr[i] = temp;
lastNonZeroFoundAt++;
}
}
}
✅ Preserves relative order
✅
Swaps in-place
✅
One-pass
⚠️ Edge Cases to Consider
Input |
Output |
Notes |
[0, 0, 0] |
[0, 0, 0] |
All zeroes |
[1, 2, 3] |
[1, 2, 3] |
No change needed |
[] |
[] |
Empty array |
[0, 1] |
[1, 0] |
Smallest non-trivial case |
📈 Time & Space
Complexity
Operation |
Value |
Time |
O(n) |
Space |
O(1) — In-place |
🧨 Interview Variants
✅ Summary
Step |
Description |
Two-pointer strategy |
Place non-zero at next available slot |
In-place operation |
No extra array required |
One-pass swap optimization |
Faster for large arrays |
Preserves order |
Interviewer will often test for this too |
Because
they test core programming logic, data handling, loops, and algorithm
efficiency.
int[] is a primitive array; Integer[] is an array of objects (wrappers). The latter allows null values and works with collections.
For fixed-size problems, use arrays. For dynamic data, ArrayList is better — but stick to arrays unless otherwise asked
Index out of bounds, mutating arrays while iterating, and forgetting that Java arrays have fixed size.
Use a Set for uniqueness or sort the array first and remove duplicates in-place using two pointers.
It’s great for sorted arrays, especially for problems involving pair sums, removing duplicates, and reversing data in-place.
Use reversal techniques or cyclic replacements to do it in O(1) space.
Linear search = O(n); binary search = O(log n), but only on sorted arrays.
Always consider edge cases: empty arrays, one element, all duplicates, etc. Optimize with hashmaps or prefix sums where needed.
Use Arrays.sort(), System.arraycopy(), Arrays.toString(), and Collections when applicable — but show the manual solution first in interviews.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)