Array -Target Sum triplet

Please find error in code as test case 2 to 5 already satisfied but test case 1 is not satisfied
#include
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,arr[1002],t;
cin>>n;
if(1<n<1000){
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cin>>t;
sort(arr,arr+n);
int i=0;

for(int a=0;a<n;a++)
{
    int l=i+1;
int r=n-1;
while(l<r)
{
	if((arr[l]+arr[r]+arr[i])>t)
	{
		r--;
	}
	else if((arr[l]+arr[r]+arr[i])<t)
	{
		l++;
	}
	else{
		cout<<arr[i]<<", "<<arr[l]<<" and "<<arr[r]<<endl;
        l++;
        r--;
	}

}
i++;
}}
return 0;

}

@sulbh579 hey sulbh please share your code through ide of the coding blocks.

@sulbh579 hey sulbh check for this case
4
1 1 1 1
3
Your output is printing twice but it should be print once.
1, 1 and 1
1, 1 and 1
Output should be
1, 1 and 1.