Move Zeros To End
Python
Medium
2 views
Problem Description
Read n integers. Move all zeros to end keeping order of non-zeros. Output result.
Input Format
First line n. Second line n integers.
Output Format
One line result.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=list(map(int,p[1:1+n]))
out=[]
zc=0
for v in a:
if v==0:
zc+=1
else:
out.append(v)
out.extend([0]*zc)
sys.stdout.write(' '.join(map(str,out)))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!