Push every 0 to the end of an array while keeping the other values in their original order, in place, with no extra array. Watch the two pointers i and k rearrange it step by step.
01 The problem
Given an array of N integers, move all the zeros to the end. The non-zero values must keep their relative order, and you should change the array in place.
For [0, 1, 0, 3, 12] the answer is [1, 3, 12, 0, 0]. The order 1 → 3 → 12 is unchanged.
Type an array to see what the program prints.
02 The code
Two loops and one extra variable. k is the next position where a non-zero value should go. The first loop packs the non-zeros to the front, and the second loop fills the rest with zeros.
package Day11;
import java.util.Scanner;
public class MoveZeroes {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
int[] arr=new int[n];
for (int i = 0; i < arr.length ; i++) {
arr[i]= sc.nextInt();
}
int k = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i]==0){
continue;
}else {
arr[k]=arr[i];
k++;
}
}
for (int i=k;i<= arr.length-1;i++){
arr[i]=0;
}
for (int el:arr){
System.out.print(el+" ");
}
}
}03 How it works
k is the write pointer
It starts at 0 and always points to the next slot that should hold a non-zero value.
i is the read pointer
It scans every element once, from left to right.
Zero? Skip it
continue leaves k where it is, so the gap stays open for the next non-zero.
Non-zero? Copy and advance
arr[k] = arr[i]; k++ packs it right after the previous non-zero, so the order is kept.
Fill the tail
After the first loop, k equals the number of non-zeros. Every slot from k to the end becomes 0.
Safe to overwrite
k never gets ahead of i, so the copy never overwrites a value that hasn't been read yet.
04 Live tracer
This runs the code above one step at a time. The array changes in place as you step.
i (read), teal pointer = k (write). Violet cells are packed non-zeros, rose cells are filled zeros. Click any table row to jump to it.| # | Phase | i | k | Action | arr |
|---|
05 Three ways to solve it
| Approach | Idea | Time | Extra space |
|---|---|---|---|
| Extra array | Copy the non-zeros into a new array, then pad it with zeros | O(N) | O(N) |
| Copy + fill (this post) | Pack non-zeros to the front with k, then zero the tail | O(N) | O(1) |
| Swap in one pass | When arr[i] != 0, swap arr[i] with arr[k] and do k++ | O(N) | O(1) |
Copy + fill reads each element once in the first loop and touches only the N − k zero slots in the second, so the total is still O(N). The swap version does it in a single loop, but each swap writes two values.
06 Quick revision
Two pointers, one array
i reads, k writes. No second array is needed.
Order is preserved
Non-zeros are copied in the same left-to-right order they were read.
k counts the non-zeros
After loop 1, k = number of non-zeros, and n − k = number of zeros.
Fill from k to the end
i = k … n − 1 all become 0.
O(N) time, O(1) space
Each loop is linear, and the only extra variable is k.
Test the edge inputs
Try no zeros [1 2 3], all zeros [0 0 0], and zeros at the start.
Comments
Post a Comment