Move all zeroes to end of array using Two-Pointers
Given an array of random numbers, Push all the zero’s of the given array to the end of the array. For example, if the given arrays is {1, 0, 2, 6, 0, 4}, it should be changed to {1, 2, 6, 4, 0, 0}. The order of all other elements should be the same.
Examples:
Input: arr[]={8, 9, 0, 1, 2, 0, 3}
Output: arr[]={8, 9, 1, 2, 3, 0, 0}
Explanation:
Swap {0 ,1} -> Resulting array {8, 9, 1, 0, 2, 0, 3}
Swap {0 ,2} -> Resulting array {8, 9, 1, 2, 0, 0, 3}
Swap {0 ,3} -> Final array {8, 9, 1, 2, 3, 0, 0}
Input: arr[]={4, 5, 0, 0, 0, 0, 6, 7}
Output: arr[]={4, 5, 6, 7, 0, 0, 0, 0}
Approach:
- Iterate the array from 0 to N.
- Keep two pointers, one for zero elements and other for non-zero elements.
- Swap every zero elements with the non-zero element that comes just after it.
C
#include<stdio.h>
void moveZerosToEnd( int arr[], int n)
{
int j=0, temp, i;
for (i=0;i<n;i++)
{
if (arr[i]!=0 && arr[j]==0)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
if (arr[j]!=0)
j+=1;
}
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; i++)
printf ( "%d " , arr[i]);
}
int main()
{
int arr[] = {8, 9, 0, 1, 2, 0, 3};
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Original array: " );
printArray(arr, n);
moveZerosToEnd(arr, n);
printf ( "\nModified array: " );
printArray(arr, n);
return 0;
}
|
C++
#include <iostream>
using namespace std;
void moveZerosToEnd( int arr[], int n)
{
int j=0, temp, i;
for (i=0;i<n;i++)
{
if (arr[i]!=0 && arr[j]==0)
{
swap(arr[i],arr[j]);
}
if (arr[j]!=0)
j+=1;
}
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = {8, 9, 0, 1, 2, 0, 3};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Original array: " ;
printArray(arr, n);
moveZerosToEnd(arr, n);
cout << "\nModified array: " ;
printArray(arr, n);
return 0;
}
|
Java
class GFG {
static void moveZerosToEnd( int arr[], int n) {
int j = 0 , i;
for (i = 0 ; i < n; i++) {
if (arr[i] != 0 && arr[j] == 0 ) {
arr = swap(arr, i, j);
}
if (arr[j] != 0 )
j += 1 ;
}
}
static int [] swap( int [] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
static void printArray( int arr[], int n) {
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 8 , 9 , 0 , 1 , 2 , 0 , 3 };
int n = arr.length;
System.out.print( "Original array: " );
printArray(arr, n);
moveZerosToEnd(arr, n);
System.out.print( "\nModified array: " );
printArray(arr, n);
}
}
|
Python3
def moveZerosToEnd(nums):
j = 0
for i in range ( len (nums)):
if nums[i]! = 0 and nums[j] = = 0 :
nums[i], nums[j] = nums[j], nums[i]
if nums[j]! = 0 :
j + = 1
def printArray(arr, n):
for i in range ( 0 , n):
print (arr[i],end = " " )
arr = [ 8 , 9 , 0 , 1 , 2 , 0 , 3 ]
n = len (arr)
print ( "Original array:" , end = " " )
printArray(arr, n)
moveZerosToEnd(arr)
print ( "\nModified array: " , end = " " )
printArray(arr, n)
|
C#
using System;
class GFG{
static void moveZerosToEnd( int []arr, int n)
{
int j = 0, i;
for (i = 0; i < n; i++)
{
if (arr[i] != 0 && arr[j] == 0)
{
arr = swap(arr, i, j);
}
if (arr[j] != 0)
j += 1;
}
}
static int [] swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
static void printArray( int []arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
public static void Main(String[] args)
{
int []arr = { 8, 9, 0, 1, 2, 0, 3 };
int n = arr.Length;
Console.Write( "Original array: " );
printArray(arr, n);
moveZerosToEnd(arr, n);
Console.Write( "\nModified array: " );
printArray(arr, n);
}
}
|
Javascript
<script>
function moveZerosToEnd(arr, n)
{
let j=0, temp, i;
for (i=0;i<n;i++)
{
if (arr[i]!=0 && arr[j]==0)
{
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
if (arr[j]!=0)
j+=1;
}
}
function printArray(arr, n)
{
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
}
let arr = [8, 9, 0, 1, 2, 0, 3];
let n = arr.length;
document.write( "Original array: " );
printArray(arr, n);
moveZerosToEnd(arr, n);
document.write( "<br>Modified array: " );
printArray(arr, n);
</script>
|
Output:
Original array: 8 9 0 1 2 0 3
Modified array: 8 9 1 2 3 0 0
Time Complexity: O(N).
Auxiliary Space: O(1)
Last Updated :
25 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...