#K55267. Reorder Array for Non-decreasing Product Sequence
Reorder Array for Non-decreasing Product Sequence
Reorder Array for Non-decreasing Product Sequence
Given an array of integers, reorder it such that the sequence \(a_i \times i\) (where \(i\) is the 0-indexed position) is in non-decreasing order. In other words, after reordering, the products of each element with its index should satisfy:
[ a_0 \times 0 \leq a_1 \times 1 \leq a_2 \times 2 \leq \cdots \leq a_{N-1} \times (N-1) ]
If there are multiple valid reorderings, output the lexicographically smallest permutation. In this problem, it turns out that simply sorting the array in ascending order yields the correct answer.
inputFormat
The input begins with an integer \(T\) representing the number of test cases. Each test case consists of two lines:
- The first line contains an integer \(N\) which is the number of elements in the array.
- The second line contains \(N\) space-separated integers.
outputFormat
For each test case, output a single line containing the reordered array. The elements should be printed as space-separated integers.
## sample4
5
3 1 2 5 4
4
8 4 6 2
4
5 5 5 5
3
1 2 3
1 2 3 4 5
2 4 6 8
5 5 5 5
1 2 3
</p>