Not satisfying all the test cases

i am unable to find my mistake in this code:

using namespace std;
#include
#include
int main()
{
int n,target;
cin>>n;
int a[n];

for(int i=0;i<n;i++)
cin>>a[i];
cin>>target;

sort(a,a+n);

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

}

@n.nishchaya2000
hello Nishchaya,
use if else- if and else otherwise it will do more than one increment / decrement which can voilate i<j condition

image

but sir if i am going to cout the i and j value in each condition its not showing any voilation

@n.nishchaya2000
for some test case it can voilate

can u pls tell me those test cases as an eg.

@n.nishchaya2000
target 13
1,3,6

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.