Arrays-target sum triplets

Solution Not passing all the test cases.

#include
#include
using namespace std;
int main() {
int i,j,k,t=0,n;
int l,r;
int a[1000];
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cin>>t;
sort(a,a+n);
for(i=0;i<n;i++)
{
l=i+1;
r=n-1;
while(l<r)
{
if(t==(a[i]+a[l]+a[r]))
{
cout<<a[i]<<", “<<a[l]<<” and "<<a[r]<<endl;
l++;
r–;
}
else if((a[i]+a[l]+a[r])>t)
{
r–;
}
else{
l++;
}
}
}
return 0;
}

Hi @shivansh.sm1
Your code is failing one test case because when you have found the triplet then you only need to increment l. Because in test cases like :
4
2 2 2 2
4
Your code prints output as 2 times 2, 2 and 2 but its correct output is 3 times 2, 2 and 2.

Here is your corrected code :

I am not able to open the code.

Refresh your page and try to open it once again if you are still unable to open it then Arrays-target sum triplets
Click on this link and there you can open that code