Target sum pair

#include<bits/stdc++.h>
using namespace std;
int main() {
int a[1000];
int l,t,s;
cin>>l;
if(l==0)
return 0;

for(int i=0;i<l;i++)
{
	cin>>a[i];

}
int index=0;
cin>>t;
sort(a,a+l);
for(int i=0;i<l;i++)
{  s=t-a[i];
  if(binary_search(a+i,a+l,s))
  {
	  auto it=lower_bound(a+i,a+l,s);
	  index=it-a;
	  cout<<a[i]<<" and "<<a[index]<<endl;

  }

}


return 0;

I am failing first test case only. whats wrong with the code? am i missing an edge case?

there are couple of mistakes

  1. lower bounds the lower index would be a+i+1
  2. you need to check if a[i] + a[index] == target

rectified code:

1 Like

yeah, got it , thanks

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.

1 Like