#C3964. Rearrange List Based on Divisor
Rearrange List Based on Divisor
Rearrange List Based on Divisor
You are given a list of integers and an integer divisor ( d ). Your task is to rearrange the list so that all the elements that are divisible by ( d ) appear at the beginning of the list, while the relative order of the numbers in each group (divisible and not divisible) remains the same as in the original list. The input contains multiple test cases.
For example, if the list is [1, 2, 3, 4, 5] and ( d = 3 ), then the number divisible by 3 is [3] (appearing first) and the remaining are [1, 2, 4, 5]. The result will be [3, 1, 2, 4, 5].
inputFormat
The first line contains an integer ( t ) representing the number of test cases. For each test case:
- The first line contains two integers ( n ) and ( d ), where ( n ) is the number of elements in the list and ( d ) is the divisor.
- The second line contains ( n ) space-separated integers representing the list.
outputFormat
For each test case, output a single line with the rearranged list, where the list elements are separated by a single space.## sample
2
5 3
1 2 3 4 5
6 2
12 3 10 8 6 5
3 1 2 4 5
12 10 8 6 3 5
</p>