Problem : NCOPRIMEN Problem - CodeChef
Solution:
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pqb priority_queue
#define pqs priority_queue<int,vi,greater >
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define light ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using vi = vector;
using vvi = vector;
using vvvi = vector;
using mii = map<int, int>;
using pii = pair<int, int>;
const long long M = 1e9 + 7;
void io() {
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“o.txt”, “w”, stdout);
#endif
}
bool comp(pii a, pii b)
{
return a.ss > b.ss;
}
bool f(int x, int y)
{
return x > y;
}
void solve()
{
int n;
cin >> n;
vi a(n);
int i;
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < n; i++)
{
if (i + 1 < n) {
int k = __gcd(a[i], a[i + 1]);
if (k == 1)
{
if (i + 2 < n)
{
int t = __gcd(a[i], a[i + 2]);
if (t == 1)
{
a[i + 1] = a[i];
}
else
{
a[i + 1] = t;
}
}
else
{
a[i + 1] = a[i];
}
}
}
}
for (auto i : a)
cout << i << " ";
cout << endl;
}
int32_t main()
{
io();
light;
int tc = 1;
cin >> tc;
while (tc--) {
solve();
}
}