Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).
Example:
Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
Output : arr[] = {1, 2, 4, 3, 5, 0, 0};
Input : arr[] = {1, 2, 0, 0, 0, 3, 6};
Output : arr[] = {1, 2, 3, 6, 0, 0, 0};
There can be many ways to solve this problem. Following is a simple and interesting way to solve this problem.
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 that run a loop which makes all elements zero from ‘count’ till end of the array.
Below is the implementation of the above approach.
Python3
def pushZerosToEnd(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
arr = [ 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 ]
n = len (arr)
pushZerosToEnd(arr, n)
print ( "Array after pushing all zeros to end of array:" )
print (arr)
|
Output
Array after pushing all zeros to end of array:
[1, 9, 8, 4, 2, 7, 6, 9, 0, 0, 0, 0]
Time Complexity: O(n) where n is number of elements in input array.
Auxiliary Space: O(1)
Method 2: Using two pointer
- Initialize a pointer variable ‘left’ to 0 and another pointer variable ‘right’ to n-1, where n is the length of the array.
- Repeat the following steps until the value of ‘left’ is less than or equal to ‘right’:
a. If the value at index ‘left’ is non-zero, increment the value of ‘left’.
b. If the value at index ‘right’ is zero, decrement the value of ‘right’.
c. If the value at index ‘left’ is zero and the value at index ‘right’ is non-zero, swap the values at these indices and then increment the value of ‘left’ and decrement the value of ‘right’.
- The above steps will move all the zeros to the end of the array and all the non-zero values to the front of the array.
Python3
def pushZerosToEnd(arr, n):
left = 0
right = n - 1
while left < = right:
if arr[left] ! = 0 :
left + = 1
elif arr[right] = = 0 :
right - = 1
else :
for i in range (left, right):
if arr[i] = = 0 :
arr[i], arr[i + 1 ] = arr[i + 1 ], arr[i]
right - = 1
arr = [ 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 ]
n = len (arr)
pushZerosToEnd(arr, n)
print ( "Array after pushing all zeros to end of array:" )
print (arr)
|
Output
Array after pushing all zeros to end of array:
[1, 9, 8, 4, 2, 7, 6, 9, 0, 0, 0, 0]
Time complexity: The time complexity of this approach is O(n), where n is the length of the array.
Auxiliary space: The space complexity of this approach is O(1), as it only uses two pointers and a few variables for swapping.
Please refer complete article on Move all zeroes to end of array for more details!
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!