In question, Arrays-Target Sum Triplets i am getting wrong output
code :-
#include
#include<bits/stdc++.h>
using namespace std;
int main() {
int n ;
cin>>n;
int a[1000];
for(int i=0; i<n; i++){
cin>>a[i];
}
int target;
cin>>target;
sort(a,a+n);
int l,r;
for(int i=0; i<n-2; i++){
l = i+1;
r = n-1;
while(l<r){
if(a[i] + a[l] + a[r] == target){
cout<<a[i]<<", “<<a[l]<<” and "<<a[r]<<endl;
}
else if (a[i] + a[l] + a[r] < target){
l++;
}
else{
r–;
}
}
}
return 0;
}
Getting wrong output
i got correct answer
code : #include #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int a[n]; for(int i=0; i<n; i++){ cin>>a[i]; } n = sizeof(a) / sizeof(a[0]); int tar; cin>>tar; int l, r; sort(a,a+n); for (int i = 0; i < n-2; i++){ l = i + 1; r = n - 1; while (l < r){ if(a[i] + a[l] + a[r] == tar){ cout<<a[i]<<", “<<a[l]<<” and "<<a[r]<<endl; l++; r–; } else if (a[i] + a[l] + a[r] < tar) l++; else r–; } } return 0; }