Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 1096 (Div. 3)/C. Snowfall
PrevNext
CodeforcesCodeforces
Pupil
Codeforces Round 1096 (Div. 3)

C. Snowfall

MathArray
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

Yousef has given you an array π‘Ž

 of π‘›

 positive integers.

Let π‘“(π‘Ž)

 denote the number of subarrays

βˆ—

 of π‘Ž

 whose product is divisible by 6

.

More formally, for every pair of indices π‘™

 and π‘Ÿ

 such that 1β‰€π‘™β‰€π‘Ÿβ‰€π‘›

, consider the subarray π‘Ž

𝑙

,π‘Ž

𝑙+1

,…,π‘Ž

π‘Ÿ

. This subarray is counted if the product of its elements is divisible by 6

.

For example, if π‘Ž=[1,6,2]

, then the subarrays whose products are divisible by 6

 are [6]

, [1,6]

, [6,2]

, and [1,6,2]

, so π‘“(π‘Ž)=4

.

Your task is to reorder the elements of the array π‘Ž

 so that π‘“(π‘Ž)

 is minimized. If there are multiple ways to do this, you may output any of them.


βˆ—

An array π‘

 is a subarray of an array π‘Ž

 if π‘

 can be obtained from π‘Ž

 by deleting several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.

Input


The first line of the input contains an integer π‘‘

 (1≀𝑑≀10

4

) β€” the number of test cases.

The first line of each test case contains an integer π‘›

 (1≀𝑛≀2β‹…10

5

) β€” the size of the array.

The second line of each test case contains π‘›

 integers π‘Ž

1

,π‘Ž

2

,…,π‘Ž

𝑛

 (1β‰€π‘Ž

𝑖

≀10

9

) β€” the elements of the array.

It is guaranteed that the sum of π‘›

 over all test cases does not exceed 2β‹…10

5

.

Output


For each test case, output the array after reordering it in such a way that π‘“(π‘Ž)

 is minimized. If there are multiple answers, you may output any of them.

Example

Input

Copy

5
6
12 7 9 4 18 5
4
3 6 2 8
7
1 10 15 20 3 6 9
5
11 14 21 2 5
3
6 6 6

Output

Copy

12 18 4 7 5 9
2 8 3 6
6 10 20 1 15 3 9
21 5 11 2 14
6 6 6

Note


In the first test case, an optimal arrangement is π‘Ž=[12,18,4,7,5,9]

. The subarrays whose products are divisible by 6

 are:

  • [12]
  • [18]
  • [12,18]
  • [18,4]
  • [12,18,4]
  • [18,4,7]
  • [12,18,4,7]
  • [18,4,7,5]
  • [4,7,5,9]
  • [12,18,4,7,5]
  • [18,4,7,5,9]
  • [12,18,4,7,5,9]

Therefore, π‘“(π‘Ž)=12

. It can be proven that no other arrangement yields a smaller value of π‘“(π‘Ž)

.

Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 1096 (Div. 3)
Previous ProblemNext Problem