The problem is to find for a 1<=n<=50000-element sequence of numbers (1 to 10^5) if such a subsequence can be chosen that the remaining elements will create another copy of the chosen subsequence. In other words, if the original sequence was made by intertwining two copies of some subsequence. If so, we have to additionally print the original subsequence. My idea was to naively seperate every other number of the same kind into two subsequences. So, for example, the first subsequence would get 1st 5, 3rd 5 and 1st 4 and the second one would get 2nd 5, 4th 5 and 2nd 4. I thought the sequence cannot be expressed as a combination of two copies like that iff it has an odd number of some numbers in it. However, the entire approach is faulty and very rarely gives good answers;
Please help me find a more clever algorithm that will actually solve the problem. My implementation of the naive approach for better understanding (C++):
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<short> arr(n);
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
}
vector<vector<unsigned short>> positions(10001, vector<unsigned short>(0));
for (int i = 0; i < n; ++i)
{
positions[arr[i]].push_back(i);
}
vector<unsigned short> out;
for (int i = 0; i <= 10000; ++i)
{
if (positions[i].size() % 2)
{
cout << "NO" << "\n";
return 0;
}
for (int j = 0; j < positions[i].size(); j += 2)
{
out.push_back(positions[i][j]);
}
}
sort(out.begin(), out.end());
cout << "YES" << "\n";
for (int i = 0; i < out.size(); ++i)
{
cout << arr[out[i]] << " ";
}
}