I have a doubt in the first div 3 contest held in CF

Revision en4, by akanksha_11, 2021-05-18 09:08:38

I am a beginner in CP and so I was solving the contest#977,which is the first div 3 contest. But I got stuck in D ques The ques is:

Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n−1 operations of the two kinds:

divide the number x by 3 (x must be divisible by 3); multiply the number x by 2. After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.

You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.

Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.

It is guaranteed that the answer exists.

Input The first line of the input contains an integer number n (2≤n≤100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤3⋅10^18) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.

It is guaranteed that the answer exists.

Sample i/p : 6 4 8 6 3 12 9

sample o/p: 9 3 6 12 4 8

Can someone please tell me what's wrong in my approach?...My code is:

#include <iostream>
#include <set>
#include <vector>
#define ll __int64
using namespace std;

int main()
{
short n;
cin >> n;
ll a[n];
set<ll> nos, fixed;
vector<ll> v;
for (short i = 0; i < n; i++)
{
cin >> a[i];
fixed.insert(a[i]);
}
nos = fixed;
short j = 0;
ll s = a[j];
v.push_back(s);
nos.erase(s);
for (short i = 0; i < n; i++)
{
if (a[i] == s)
continue;
if ((s % 3 == 0) && (nos.count(s / 3)))
{
s /= 3;
v.push_back(s);
nos.erase(s);
}
if (nos.count(s * 2))
{
s = s * 2;
v.push_back(s);
nos.erase(s);
}
if ((i + 1 == n) && (v.size() != n))
{
v.clear();
i = -1;
j++;
s = a[j];
v.push_back(s);
nos = fixed;
nos.erase(s);
}
}
for (short i = 0; i < n; i++)
{
cout << v[i] << endl;
}
return 0;
}


#### History

Revisions

Rev. Lang. By When Δ Comment
en4 akanksha_11 2021-05-18 09:08:38 1491
en3 akanksha_11 2021-05-18 09:01:44 50 Tiny change: 'n~~~~~\n\n ' -> 'n~~~~~\n\nUPD: Instead of downvoting...please help me out!\n '
en2 akanksha_11 2021-05-18 07:27:41 20
en1 akanksha_11 2021-05-18 07:26:54 1402 Initial revision (published)