There is some problem in the output

there is some problem in the output… sir pls tell whats the problem with the code…

#include
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,i,j,k,target,arr[1000];
cin>>n;

//reading array elements
for(i=0;i<n;i++){
	cin>>arr[i];
}	

cin>>target;

for(i=0;i<n-2;i++){
	for(j=i+1;j<n-1;j++){
		for(k=j+1;k<n;k++){
		        if(arr[i]+arr[j]+arr[k]==target){
		        	
		        	if (arr[i] > arr[j])
					    swap(arr[i], arr[j]);
					if (arr[i] > arr[k])
					    swap(arr[i], arr[k]);
					if (arr[j] > arr[k])
					    swap(arr[j], arr[k]);	        	
		        	
		        	
		        	cout<<arr[i]<<","<<" "<<arr[j]<<" "<<"and"<<" "<<arr[k]<<endl;
		        }
	    }
   }
}

return 0;
}

@Vaibhav277 better way to sort the array, then you will not need to swap the array elements.
Also this approach is O(n^3) and may not pass because the constraints are n<10^3.
So try optimizing your code.

no the thing is that when i am running the code only 3 outputs are being displayed and not all 4 … where is it going wrong over there

@Vaibhav277
As your soltuion is trying to selecting all combinations of triplets by all possibles combinations of indexes in the array.

but the problem is because you are swapping the index(for trying to sort the numbers), then it will gonna skip some cases.

You can check this thing, if you don’t use any swapping, then all the 4 combinations are priniting.

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.