Write a Python program to push all zeros to the end of a given list

Python Challenges: Push all zeros to the end of a list

Last update on February 26 2020 08:09:27 (UTC/GMT +8 hours)

In-place Move Zeros to End of List in Python

PythonServer Side ProgrammingProgramming

Suppose we have a list of numbers nums, we have to put all the zeros to the end of the list by updating the list in-place. And the relative ordering of other elements should not be changed. We have to try to solve this in O(1) additional space.

So, if the input is like [2,0,1,4,0,5,6,4,0,1,7], then the output will be [2, 1, 4, 5, 6, 4, 1, 7, 0, 0, 0]

To solve this, we will follow these steps −

  • if size of L is same as 0, then
    • return a blank list
  • k := 0
  • for i in range 0 to size of L, do
    • if L[i] is not same as 0, then
      • L[k] := L[i]
      • k := k + 1
  • for j in range k to size of L, do
    • L[j] := 0
  • return L

Let us see the following implementation to get better understanding −

PYTHON PROGRAM- PUSH

Write a Python program to push all zeros to the end of a given lista. The order of the elements should not change. Input Format: Elements of the listawith each element separated by a space. Output Format: Elements of the modified list with each element separated by a space. After the last element, there should not be any space. Example: Input: 0 2 3 4 6 7 10 Output: 2 3 4 6 7 10 0 Explanation: There is one zero in the list. After pushing it at the end the elements of the list becomes 2 3 4 6 7 10 0. The order of other elements remains the same.

python input python3 user push
Krishnanshu Dey
Write a Python program to push all zeros to the end of a given list

0

Write a Python program to push all zeros to the end of a given list a. The order of the elements should not change. Input Format: Elements of the list a with each element separated by a space. Output Format: Elements of the modified list with each element separated by a space. After the last element, there should not be any space. Example: Input: 0 2 3 4 6 7 10 Output: 2 3 4 6 7 10 0 Explanation: There is one zero in the list. After pushing it at the end the elements of the list becomes 2 3 4 6 7 10 0. The order of other elements remains the same.

Saksham Turki
Write a Python program to push all zeros to the end of a given list

Move all zeroes to end of array

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, 0}; Input : arr[] = {1, 2, 0, 0, 0, 3, 6}; Output : arr[] = {1, 2, 3, 6, 0, 0, 0};

Move all zeroes to end of array using List Comprehension in Python

Given an array of random numbers, Push all the zeros 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).

Examples:

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]

C


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
// Function to move all zeros present in an array to the end
void reorder(int A[], int n)
{
// `k` stores the index of the next available position
int k = 0;
// do for each element
for (int i = 0; i < n; i++)
{
// if the current element is non-zero, put the element at the
// next free position in the array
if (A[i] != 0) {
A[k++] = A[i];
}
}
// move all 0's to the end of the array (remaining indices)
for (int i = k; i < n; i++) {
A[i] = 0;
}
}
int main(void)
{
int A[] = { 6, 0, 8, 2, 3, 0, 4, 0, 1 };
int n = sizeof(A) / sizeof(A[0]);
reorder(A, n);
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
return 0;
}

DownloadRun Code

Output:

6 8 2 3 4 1 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.