Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84debe4

Browse files
authored
Update zeros_at_the_end.py
1 parent 92e85b8 commit 84debe4

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

zeros_at_the_end.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
"""Zeros_at the end.ipynb
3-
4-
Automatically generated by Colaboratory.
5-
6-
Original file is located at
7-
https://colab.research.google.com/drive/1UQ9TjradMeWthUYJrSAjRBG5pWJiFhtR
8-
9-
**Move all zeros present in an array to the end**
2+
"""
3+
**Move all zeros present in an array to the end**
104
115
Q. Given an integer array, move all zeros present on it to the end. The solution should maintain the relative order of items in the
126
array and should not use constant space.
@@ -41,13 +35,11 @@ def shiftAllZerosToEnd(arr):
4135
shiftAllZerosToEnd(arr)
4236
print(arr)
4337

44-
"""SOLUTION 2: Coders trick
45-
38+
"""
39+
SOLUTION 2: Coders trick
4640
First we count number of zeros then we iterate through array and remove all the zeros and append zeros at the end of array for every zeros removed.
47-
4841
Time complexity of this solution is greater then O(n) but no extra space is required.
4942
50-
5143
"""
5244

5345
arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9]
@@ -57,11 +49,12 @@ def shiftAllZerosToEnd(arr):
5749
arr.append(0)
5850
print(arr)
5951

60-
"""SOLUTION 3: Mathematical Coder!
52+
"""
53+
SOLUTION 3: Mathematical Coder!
6154
Sort the array by setting key to bool and reverse is true. In python the value of true is 1 and false is 0. When there is any number except 0 it returns true and false for zero.
6255
Time complexity of this solution is greater then O(nlogn) but no extra space is required.
6356
"""
6457

6558
arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9]
6659
arr.sort(key = bool, reverse = True)
67-
print(arr)
60+
print(arr)

0 commit comments

Comments
 (0)