Target sum triplet

#include
#include<unordered_map>
#include
using namespace std;
void findtriplets(int* ar,int n,int target)
{unordered_map<int,int>mp;
for(int i=0;i<n;i++)
{
mp[ar[i]]++;
}

for(int i=0;i<n-2;i++)
{
	for(int j=0;j<n-1;j++)
	{
		int r=target-(ar[i]+ar[j]);
		if(mp.find(r) != mp.end())
		{
			cout<<ar[i]<<", "<<ar[j]<<" and "<<r<<endl;	
		}
	
	}
	
}

}
int main() {
int n,target;
int ar[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>ar[i];
}
cin>>target;
sort(ar, ar+n);
findtriplets(ar,n,target);
}

It is printing all duplicate triplets where target is matched.

hi @sharmasrishti891
do it using 2 pointer approach to resolve the issue of duplicates…
refer this https://ide.codingblocks.com/s/645124

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.