We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution.
POTD 31 October: Move all zeroes to end of array
Given an array arr[] of n positive integers. Push all the zeros of the given array to the right end of the array while maintaining the order of non-zero elements. Do the mentioned change in the array in-place.
Example:
Input: arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
Output: arr[] = {1, 2, 4, 3, 5, 0, 0, 0};
Input: arr[] = {1, 2, 0, 0, 0, 3, 6};
Output: arr[] = {1, 2, 3, 6, 0, 0, 0};
The idea is to use the two pointer approach, as we know for any non zero element its position can be its current position or at the previous index where 0 is present, so when we encounter a non zero element we will swap the values at both the index and increment both of them.
Step-by-step approach:
- Initialize a variable j equal to 0.
- Run a loop i from 0 to n.
- if the value at current index is non zero then swap the values at index i and j and increment j by 1.
below is the implementation of the approach.
C++
class Solution {
public :
void pushZerosToEnd( int arr[], int n)
{
int j = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] != 0) {
swap(arr[j], arr[i]);
j++;
}
}
}
};
|
Java
class Solution {
void pushZerosToEnd( int [] arr, int n)
{
int j = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] != 0 ) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
j++;
}
}
}
}
|
Python3
class Solution:
def pushZerosToEnd( self , arr, n):
j = 0
for i in range (n):
if arr[i] ! = 0 :
arr[j], arr[i] = arr[i], arr[j]
j + = 1
|
Time Complexity: O(N), As we are running a nested loop of size N, So overall time complexity becomes O(N).
Auxiliary Space: O(1), As we are not using any extra space.
Traverse the given array ‘arr’ from left to right. While traversing, maintain count of non-zero elements in array. Let the count be ‘count’. For every non-zero element arr[i], put the element at ‘arr[count]’ and increment ‘count’. After complete traversal, all non-zero elements have already been shifted to front end and ‘count’ is set as index of first 0. Now all we need to do is run a loop that makes all elements zero from ‘count’ till end of the array.
Below is the implementation of the above approach.
C++
class Solution {
public :
void pushZerosToEnd( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] != 0) {
arr[count++]
= arr[i];
}
}
while (count < n) {
arr[count++] = 0;
}
}
};
|
Java
class Solution {
void pushZerosToEnd( int [] arr, int n) {
int count = 0 ;
for ( int i = 0 ; i < n; i++)
if (arr[i] != 0 )
arr[count++] = arr[i];
while (count < n)
arr[count++] = 0 ;
}
}
|
Python3
class Solution:
def pushZerosToEnd( self ,arr, n):
count = 0
for i in range (n):
if arr[i] ! = 0 :
arr[count] = arr[i]
count + = 1
while count < n:
arr[count] = 0
count + = 1
|
Time Complexity: O(n) where n is the size of elements of the input array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...