There is an array initially containing n numbers. then each of the numbers of the array is multiplied by 2. Now the array is 2 * n size and each element of the array gets shuffled. You are given the shuffled array of size 2 * n. You have to restore the original array.
Show Answer
bool restoreDouble(vector<int> input,vector<int>& output){
int n = input.size();
map<int,int> marked;
sort(input.begin(),input.end());
for(int i=0;i<n;i++){
if( marked[ input[i] ] == 0 ) {
output.push_back( input[i] );
marked[ 2*input[i] ] ++;
}else{
marked[ input[i] ]--;
}
}
for( auto entry:marked ){
if( entry.second != 0 ) return false;
}
return true;
}