Arrays Target Sum Triplets

1st Test case time exceeded, rest all passed.

#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
int t;
cin>>t;
int i=0;

while(i<n-1){
int j=i+1;
while(j<n){
	for(int x=0;x<n;x++){
		if(arr[i]+arr[j]+arr[x]==t && arr[j]<arr[x]){
			cout<<arr[i]<<", "<<arr[j]<<" and "<<arr[x]<<endl;
			break;
		}
	}
	j++;
}
i++;
}
return 0;

}

U have used a brute force approach whose complexity is O(N^3)… hence it give TLE… u will have to optimize ur approach which can be done using 2 pointer method…
Reference Code

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.