Saved the code as well as this is the output

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

}
return 0;

}

hello @saxena.anukrati15

in this else statement dont break instead increment l;

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.