#K7366. Warehouse Optimization

    ID: 34025 Type: Default 1000ms 256MiB

Warehouse Optimization

Warehouse Optimization

You are given a warehouse layout represented by an array of integers. Each integer represents the height of a stack at that position. A value of 0 indicates an empty space.

Your task is to optimize the warehouse arrangement by shifting all non-zero elements to the left while preserving their relative order and filling the remaining positions with 0. Formally, for an array \(a = [a_1, a_2, \dots, a_N]\), you need to produce another array \(b\) such that:

\(b = [\text{non-zero elements of } a, 0, 0, \dots, 0]\)

You will be given \(T\) test cases. For each test case, the first line contains an integer \(N\) (the number of positions in the warehouse), followed by a line with \(N\) space-separated integers representing the current configuration.

Print the optimized configuration for each test case on a separate line, with the numbers separated by a single space.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
N1
a11 a12 ... a1N1
N2
a21 a22 ... a2N2
...
NT
aT1 aT2 ... aTNT

Where:

  • T is the number of test cases.
  • For each test case, N is the number of positions in the warehouse.
  • aij are the stack heights (integers) at each position.

outputFormat

For each test case, output the optimized warehouse configuration on a separate line. The configuration should be printed as N space-separated integers, with all non-zero values shifted to the left (preserving their order) and the remaining positions filled with 0.

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

7 0 0 0 1 2 3 1 2 0 0 1 2 4 0 0 0

</p>