Time limit exceeded

my code is showing tle in 3 cases and running fine in other cases
#include
#include
using namespace std;
int main(){
int n,sum;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cin>>sum;
sort(a,a+n);
for(int i=0;i<n;i++){
int l=i+1;
int r=n-1;
while(l<r){
if(a[i]+a[l]+a[r]>sum){
r–;
}
else if(a[i]+a[l]+a[r]<sum){
r++;
}
else if((a[i]+a[l]+a[r])==sum){
cout<<a[i]<<", “<<a[l]<<” and "<<a[r]<<endl;
l++;
r–;
}
}
}
return 0;
}

Check this condition
else if(a[i]+a[l]+a[r]<sum){
r++; // Here it must be l++
}
Correct this and then check.
Also remove r-- from case else if((a[i]+a[l]+a[r])==sum)

If still there remain some issue with your code then
Save your code on ide.codingblocks.com and share its 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.