You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
12
6
array and should not use constant space.
@@ -41,13 +35,11 @@ def shiftAllZerosToEnd(arr):
41
35
shiftAllZerosToEnd(arr)
42
36
print(arr)
43
37
44
-
"""SOLUTION 2: Coders trick
45
-
38
+
"""
39
+
SOLUTION 2: Coders trick
46
40
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
-
48
41
Time complexity of this solution is greater then O(n) but no extra space is required.
49
42
50
-
51
43
"""
52
44
53
45
arr= [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9]
@@ -57,11 +49,12 @@ def shiftAllZerosToEnd(arr):
57
49
arr.append(0)
58
50
print(arr)
59
51
60
-
"""SOLUTION 3: Mathematical Coder!
52
+
"""
53
+
SOLUTION 3: Mathematical Coder!
61
54
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.
62
55
Time complexity of this solution is greater then O(nlogn) but no extra space is required.
0 commit comments