#include
#include
using namespace std;
int main() {
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int key;
cin>>key;
sort(arr,arr+n);
for(int i=0;i<=n-3;i++){
key=key-arr[i];
int s=i+1;
int e=n-1;
while(s0){
if(arr[s]+arr[e]==key){
cout<<arr[i]<<", “<<arr[s]<<” and "<<arr[e]<<endl;
s++;
e–;
}
else if(arr[s]+arr[e]>key)
e–;
else
s++;
}
}
return 0;
}
in the test case last output is showing 2,3 and 4 instead of 2,3 and 5
please explain why
Array- Target Sum triplet
@mayank_kumar
hello mayank.
pls save ur code on -> https://ide.codingblocks.com/ and share the generated link
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.