array-Target Sum problem please check my code whats the issue i can't able to debug?

#include
#include
using namespace std;
int main() {
int n;
int a[1000];
cin>>n;

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

int sum;
cin>>sum;

sort(a,a+n); 
// for(int i=0;i<n;i++){
//     cout<<a[i]<<" ";
// }

for(int i=0;i<n;i++){

    int l = i+1;
    int r = n-1;
    int newsum = sum - i;//10-1
    while (l < r) { 
        if (a[l] + a[r] == newsum) {
            cout<<a[i]<<","<<a[l]<<"and"<<a[r]<<endl;
        }     
        else if (a[l] + a[r] < newsum) {
            l++; 
        }
        else {
            r--;
        } 
    } 
}
return 0;

}

Sudhanshu, Your code is not working, I would recommend you to follow the native approach using 3 loops, and then check if ar[i]+ar[j]+ar[k]==target or not, if yes print else the loop will continue…

Since, you arent replying anything, I am marking this doubt as resolved, You can reopen it if you still face any issues.