C Program
#include<stdio.h> int main() { int a[100], n, i, j=0; scanf("%d", &n); for(i=0; i<n; i++) scanf("%d", &a[i]); for(i=0; i<n; i++) if(a[i]) a[j++] = a[i]; while(j<n) a[j++] = 0; for(i=0; i<n; i++) printf("%d ", a[i]); }
C Output
Input: 7 0 1 0 3 12 0 5 Output: 1 3 12 5 0 0 0
C++ Program
#include<iostream> using namespace std; int main() { int a[100], n, j=0; cin >> n; for(int i=0; i<n; i++) cin >> a[i]; for(int i=0; i<n; i++) if(a[i]) a[j++] = a[i]; while(j < n) a[j++] = 0; for(int i=0; i<n; i++) cout << a[i] << " "; }
C++ Output
Input: 6 0 2 0 4 0 6 Output: 2 4 6 0 0 0
JAVA Program
import java.util.*; class Z { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), arr[] = new int[n], j = 0; for(int i=0; i<n; i++) arr[i] = sc.nextInt(); for(int i=0; i<n; i++) if(arr[i] != 0) arr[j++] = arr[i]; while(j < n) arr[j++] = 0; for(int x : arr) System.out.print(x + " "); } }
JAVA Output
Input: 5 0 0 7 1 0 Output: 7 1 0 0 0
Python Program
a = list(map(int, input().split())) nz = [x for x in a if x != 0] print(*(nz + [0]*(len(a)-len(nz))))
Python Output
Input: 1 0 2 0 3 0 Output: 1 2 3 0 0 0
In-Depth Learning – Entire Concept in Paragraphs
What is "Move Zeros to the End"?
The work is to take an array of integers and rearrange it such that all the 0s are shifted to the end, and the positions of non-zero elements remain the same. For instance, [0, 1, 0, 3, 12] should be rearranged to [1, 3, 12, 0, 0].
This is a critical instance of in-place array manipulation, something to do when memory is scarce or optimization is needed.
How the Code Works
We employ a two-pointer or index-shifting method:
Scan the array once.
For every non-zero element, move it to the beginning (a[j++] = a[i]).
Once all non-zero elements have been moved, fill the rest with 0.
This keeps the original order for non-zero values and places all zeros at the end.
In Python, we do it more briefly by:
Constructing a new list comprising non-zero values.
Appending the necessary number of zeros at the end to preserve length.
Example
Input:
[0, 4, 0, 2, 0, 8]
Process:
Shift non-zeros: [4, 2, 8]
Append zeros: [4, 2, 8, 0, 0, 0]
Output:
[4, 2, 8, 0, 0, 0]
This keeps the order of 4, 2, 8 and puts all the 0s at the end.
Real-Life Analogy
Suppose you're sorting a tray of stuff, with 0 denoting empty boxes and non-zero as filled boxes. You want to bring all the empty boxes to the end so that only the filled boxes are towards the front, retaining their order.
Or in an elevator simulation: zeros are people not pushing a floor button — you want to service real requests (non-zeros) first and push zeros to the end.
Where and When Is It Used?
It is applied in:
Optimizing space within data arrays
Reordering datasets with missing values (0s)
Frontend UI logic (e.g., reordering items dynamically)
Gaming grids (such as 2048 game logic)
Interview and coding challenge questions
Frequently interviewed by Amazon, Google, Wipro, and TCS.
Time and Space Complexity
Operation\tComplexity
Time\tO(n) — single pass through array
Space\tO(1) — in-place (except Python, which allocates a new list)
It is one of the most space-efficient in-place modification algorithms.
Python-Specific Advantage
Python facilitates nice solutions using list comprehensions:
python
a = [0, 3, 0, 1]
print([x for x in a if x != 0] + [0]*a.count(0))
This is readable, concise, and perfect for rapid prototyping or interviews.
SEO-Optimized Natural Paragraph for Ranking
Want to shift all zeros to the end of an array in C, C++, Java, or Python? This article has the most efficient and shortest of solutions to this interviewer favorite problem. Find out how to re-arrange arrays such that all non-zero elements retain their original order and zeros are shifted to the end — without using additional space. This task is frequently asked in coding rounds by major tech companies and helps improve your understanding of array traversal, in-place swapping, and memory-efficient logic. Whether you’re a beginner or prepping for a job interview, mastering this logic is essential.
Social Plugin