Move Zeros to End
JavaScript
Easy
6 views
Problem Description
Given an array, move all zeros to the end, but keep the order of non-zero items same. Print the updated array.
Input Format
One line: n then n integers.
Output Format
One line: updated array.
Official Solution
const fs=require('fs');const p=fs.readFileSync(0,'utf8').trim().split(/\\s+/);if(!p[0])process.exit(0);let i=0;const n=Number(p[i++]);let out=[];let z=0;for(let j=0;j<n;j++){const v=Number(p[i++]);if(v===0)z++;else out.push(v);}while(z--)out.push(0);process.stdout.write(out.join(' '));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!