Idk whats wrong

#include<bits/stdc++.h>
using namespace std;
void tripletsum(int a[],int n,int key){
int i=0;
while(i<n){

	int p=i+1;
int j=n-1;
while(i+p<=j){
	//5 7 9 1 2 4 6 8 3
	//1 2 3 4 5 6 7 8 9

if(a[i]+a[p]+a[j]==key){
cout<<a[i]<<", “<<a[p]<<” and "<<a[j]<<endl;
p++ and j-- ;
}
if(a[i]+a[p]+a[j]>key){
j–;
}
if(a[i]+a[p]+a[j]<key){
p++;
}
}
i++;

}
}
int main() {
int n;
cin>>n;
auto a =new int [n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int key;
cin>>key;
sort(a,a+n);
tripletsum(a,n,key);
delete[]a;
}

hi @shivamm2110,
u need to print unique triplet
refer https://ide.codingblocks.com/s/656672 ive commented the code

but sir the code that i wrote is giving unique triplets.
kindly once look and run it.

Hi @shivamm2110 send the code on ide.codingblocks.com

sir i have sent the code kindly check

hi @shivamm2110 try this test case
5
1 1 1 1 1
3

Thanks sir i understood my error but is there anyway which i can correct my code,
so that the duplicates are removed.

hi @shivamm2110 ive send the code commented also u can read it thats how u need to skip the same value do a dry run

Thanks for helping sir

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.

1 Like