Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be51eb3

Browse files
author
Amogh Singhal
authored
Create move_zeros_to_end.py
1 parent b831a42 commit be51eb3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎move_zeros_to_end.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Given an array of integers we need to move
2+
# all the zeroes to the end and maintain the
3+
# order of rest of the elements. Needless to
4+
# say it should be an in-place solution
5+
6+
def move_zero_to_end(arr):
7+
count = 0
8+
9+
for i in arr:
10+
if i != 0:
11+
arr[count] = i
12+
count = count + 1
13+
14+
for i in range(count, len(arr)):
15+
arr[i] = 0
16+
17+
return arr
18+
19+
20+
21+
22+
array = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9]
23+
res = move_zero_to_end(array)
24+
print(res)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /