I have been getting a WA on this question idk why, please have a look if you could help
Problem Link:
https://codeforces.com/problemset/problem/1401/C
My Solution:
I compared the given sequence with its sorted version to know which elements are not at the proper places and the stored those elements.
I checked if all of those elements returned gcd with the minimum element as the minimum element itself.
If all the elements can do that, then the given array can reach the sorted form otherwise NO
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
void solve(){
int n;
cin>>n;
int a[n]={0};
int b[n]={0};
int min1=100001;
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
if(a[i]<min1)min1=a[i];
}
sort(b, b+n);
// for(int i=0;i<n;i++)cout<<a[i]<<" ";
// for(int i=0;i<n;i++)cout<<b[i]<<" ";
vector<int> v;
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])v.push_back(a[i]);
}
int x=v.size();
int flag=0;
for(int i=0;i<x;i++)
{
if(gcd(v[i],min1)!=min1)
{
flag=1;
break;
}
}
if(flag==1)cout<<"NO\n";
else cout<<"YES\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
/* #ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif */
int t=1;
cin>>t;
while(t--) solve();
return 0;
}