#C3645. Sort Non-Zero Integers and Move Zeros to the End

    ID: 47095 Type: Default 1000ms 256MiB

Sort Non-Zero Integers and Move Zeros to the End

Sort Non-Zero Integers and Move Zeros to the End

You are given an array of integers. Your task is to sort all the non-zero elements of the array in ascending order, and then move all the zeros to the end of the array. In other words, if the input array is \(nums\), let \(n = |nums|\). First, extract all elements \(x\) such that \(x \neq 0\) and sort them in increasing order. Then append the zeros (if any) to the end of this sorted list.

This problem requires careful handling of edge cases such as an empty array, an array composed entirely of zeros, or an array with only one element.

inputFormat

The input is given in two lines:

  • The first line contains an integer \(n\) (\(0 \le n \le 10^5\)) that represents the number of elements in the array.
  • The second line contains \(n\) space-separated integers.

outputFormat

Output the rearranged list on a single line. The non-zero integers should appear in ascending order followed by all the zeros. Each number should be separated by a single space. If the list is empty, output nothing.

## sample
6
0 2 3 0 1 4
1 2 3 4 0 0