Problem in arrays-target sum pairs

one test case is failing. whats wrong in the code?

#include
#include
using namespace std;

void pairs(int *a, int n, int target)
{
sort(a,a+n);
int l=a[0];
int r=a[n-1];

while(l<r)
{
    if(l+r==target)
    {
        cout<<l<<" and "<<r<<endl;
        l++;
        r--;
    }
    else if(l+r>target)
    {
        r--;
    }
    else l++;
}

}

int main() {
int n,target;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];

 cin>>target;
 
pairs(a,n,target); 


return 0;

}

@ankityadav943 hey ankit your code is giving wrong output for
6
-3
1
4
3
6
8
9
your output is
1 and 8
2 and 7
3 and 6
4 and 5
but output is
1 and 8
3 and 6

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.