#include <iostream>
#include <algorithm>
using namespace std;
void sumtriplet(int a[], int n, int target)
{
int k = 0;
int t = target;
while (k < n)
{
t = target - a[k];
int i = k + 1;
int j = n - 1;
while (i < j)
{
if ((a[i] + a[j]) == t)
{
cout << a[k] << ", " << a[i] << " and " << a[j] << endl;
i++;
j--;
}
else
{
if (a[i] + a[j] < t)
{
i++;
}
else
{
j--;
}
}
}
k++;
}
}
int main()
{
int n;
cin >> n;
int a[100] = {0};
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int target;
cin >> target;
sort(a, a + n);
sumtriplet(a, n, target);
return 0;
}
Cant pass all test caes
Code is correct just change the size of array according to constraits code will pass all testcases
Modified code
#include <iostream>
#include <algorithm>
using namespace std;
void sumtriplet(int a[], int n, int target)
{
int k = 0;
int t = target;
while (k < n)
{
t = target - a[k];
int i = k + 1;
int j = n - 1;
while (i < j)
{
if ((a[i] + a[j]) == t)
{
cout << a[k] << ", " << a[i] << " and " << a[j] << endl;
i++;
j--;
}
else
{
if (a[i] + a[j] < t)
{
i++;
}
else
{
j--;
}
}
}
k++;
}
}
int main()
{
int n;
cin >> n;
int a[10000] = {0};
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int target;
cin >> target;
sort(a, a + n);
sumtriplet(a, n, target);
return 0;
}
i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course
