Given a target K, and an array of distinct elements, the task is to remove all pairs from the array which sums to K.
This is the approach I followed
for(i=0;i<N;i++)
cin>>array[i];
cin>>K;
map<int, int> mp;
for(i=0;i<N;i++)
{
if(mp.find(array[i])==mp.end())
mp[array[i]] = 1;
else
mp[array[i]]++;
}
Logic for deletion
for(i=0;i<N;i++)
{
if(mp[K-array[i]]>0)
{
mp.erase(K-array[i]);
mp.erase(array[i]);
}
}
Print output :
cout<<mp.size();
Input :
array = 6 5 4 2 1 0
K = 6
Program output
4
Expected Output
0