Unable to pass all the testcases

Unable to pass all the testcases

@1997gautam.shah you have to display the three numbers in increasing order. did you take care of this condition?

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

void triplet (int a[],int n, int sum)
{ sort(a,a+n);

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

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

int sum;
cin>>sum;

triplet(a,n,sum);
return 0;

}

@1997gautam.shah for
4
1 2 3 4
5
your o/p 1 2 2 which is wrong as 2 is present once check this

I have given the condition while (i<j) so it should not print 1 2 2

@1997gautam.shah but bro run your code for it its coming
dry run your code you will get the error

I have try that, According to me It should not be the ouput. Please help me to understand the error.

@1997gautam.shah corrected you have to use if else if


bro is it clear now?
if yes dont forget to hit like and mark resolved :smiley:

Ok got it! Thankyou :slight_smile:

@1997gautam.shah np just mark resolved :smiley:

Done…