-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathMoveZerosToEnd.rb
27 lines (24 loc) · 958 Bytes
/
MoveZerosToEnd.rb
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
#Given an array of random numbers, Push all the zero’s of a given array to the end of the array
#Time Complexity: O(n),Auxiliary Space: O(1)
##Algorithm:
#Traverse the given array from left to right andmaintain count of non-zero elements in array.
# 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 run a loop which makes all elements zero from ‘count’ till end of the array zero.
def move_zeros(a)
len=a.length
count=0
for i in 0...len
if a[i]!=0
a[count]=a[i]
count+=1
end
end
# Loop from count to len-1 and fill it with zeros
while count<len
a[count]=0
count+=1
end
return a
end
move_zeros([1,2,0,3,0,4,0,5,0,0,6]) # => [1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0]