Target Sum Triplets code

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

}
return 0;
}

sir what is problem in this code cant understand

@piyush_sharma let me check your code.

@piyush_sharma hey l and r will change inside the foor loop these are not fixed,l=row+1 and r=n-1,see this changes
for ( int i = 0; i < n - 1; i++) {

// initialize left and right

int l = i + 1;

int r = n - 1;

int x = arr[i];

while (l < r) {

if (x + arr[l] + arr[r] == sum) {

// print elements if it's sum is given sum.

printf ( "%d %d %d\n" , x, arr[l], arr[r]);

l++;

r--;

}

// If sum of three elements is less

// than 'sum' then increment in left

else if (x + arr[l] + arr[r] < sum)

l++;

// if sum is greater than given sum, then

// decrement in right side

else

r--;

}

}