How to solve failed test case

#include
#include
using namespace std;
void targetsumpair(int *a,int n,int target)
{
int i=0;
int j=n-1;
while (i<j)
{
if (a[i]+a[j]==target)
{
cout<<a[i]<<" and "<<a[j]<<endl;
i++;j++;
}
else
{
if (a[i]<target)
{
j–;
}
else
{
i++;
}

}

}

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

return 0;

}

@sagar_aggarwal
hello Sagar,
a)
image
it should be j- -;
b)
image

if a[i] + a[j] < target then i++;
otherwise j- -;

thank you for solving doubt

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.