Problem Link
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t, n, index, count;
bool isPossible;
cin >> t;
while(t--)
{
cin >> n;
pair<int, int> arr[n];
int b[n];
int result[n];
index = 0;
isPossible = true;
for(int i = 0; i < n; i++)
{
cin >> arr[i].first;
arr[i].second = i;
}
sort(arr, arr + n);
/* We can have atmost floor(n/2) same colors and still colors of marker and cap can be different. If number of same colors > floor(n/2) then atleast one pen will have same colored cap. So we are checking if color at position i and i + (n + 1) / 2 position is same or not.
*/
for(int i = 0; i <= n / 2; i++)
{
if(arr[i].first == arr[(i + ((n + 1) / 2)) % n].first)
{
isPossible = false;
break;
}
}
if(isPossible)
{
cout << "Yes\n";
for(int i = 0; i < n; i++)
{
b[arr[i].second] = arr[(i + ((n + 1) / 2)) % n].first;
}
for(int i = 0; i < n; i++)
{
cout << b[i] << " ";
}
cout << endl;
}
else
{
cout << "No\n";
}
}
return 0;
}
Comments
Post a Comment