#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int i,a[n];
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int sum;
cin>>sum;
int l=0,r=n-1;
while(l<r)
{
if(a[l]+a[r]==sum)
cout<<a[l]<<" and "<<a[r]<<endl;
else if(a[l]+a[r]<sum)
l++;
else
r–;
}
return 0;
}
This code is not getting accepted for the problem target sum pair
Hi @agarwalarjit.agarwal
in dis case
if(a[l]+a[r]==sum)
cout<<a[l]<<" and "<<a[r]<<endl;
add l++;r–; since once u have found the sum, u move both the pointer to the next position.
Hope dis helps.
If something is still unclear, feel free to post ur doubt here
it is solved, thanks for the assistance