Changes in code

#include
#include
using namespace std;
int main() {
int n;
cin>>n;
int a[1000]={0};
for(int i=0;i<n; i++){
cin>>a[i];
}
sort(a,a+n);
int key;
cin>>key;
int i=0;
int left=i+1;
int right=n-1;
for(i=0;i<n;i++){
while(left<right){

	    int current_sum=a[i]+a[left]+a[right];
		
	    if(current_sum>key){
		    right--;
	
	    }else if(current_sum<key){
		    left++;
	    }else if(current_sum==key){
		    cout<<a[i]<<","<<" "<<a[left]<<" "<<"and"<<" "<<a[right]<<endl;
		    left++;
		    right--;
	    }
	}
}
return 0;

}

@neelrai906 define your left and right inside for loop and before while loop.

1 Like

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.