Target sum triplet

//this is my solution for the target sum triplet problem. It is not able to pass all the test cases. Why??

#include
#include
using namespace std;
int main() {
int n; cin>>n;
int arr[n];
for(int i=0; i<n; i++)
[[]] cin>>arr[i];
int key; cin>>key;
sort(arr, arr+n);
for(int i=0; i<n-2; i++)
{

	int a=i+1;
	int b=n-1;
	while(a<b)
	{
		if((arr[i]+arr[a]+arr[b])>key)
		b--;
		if((arr[i]+arr[a]+arr[b])<key)
		a++;
		if(arr[i]+(arr[a]+arr[b])==key)
		{
			cout<<arr[i]<<", "<<arr[a]<<" and "<<arr[b]<<endl;
			a++; b--;
		}

	}

}

return 0;

}

Please save you code on ide.codingblocks.com and then 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.