Target sum triplet code giving stack smashing detected error

#include
using namespace std;
#include

int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int t;
cin>>t;
for(int i=0;i<=n-3;i++)
{
for(int j=i+1;j<=n-1;j++)
{
for(int k=j+1;k<=n;k++)
{
if(a[i]+a[j]+a[k]==t)
{
int b[3];
b[0]=a[i];
b[1]=a[j];
b[2]=a[k];
sort(b,b+n);
cout<<b[0]<<", “<<b[1]<<” and "<<b[2]<<endl;
}
}
}
}
return 0;
}

Hello @varu,

Click on this link to understand stack smashing detected error better.

BTW, the approach you are using is brute force and may cause TLE for the given constraints in the question.
Please, refer to the hint video.

Hope, this would help.
Give a like if you are satisfied.

but i had compiled this code in other compiler (dev c++)

it is always assuming b[0] as 0 every time

ok,i have read about stack smashing error but can you tell me where it is in my code?

Hello @varu,

Though it is not showing that error to me,
I have corrected the mistake that was leading to this error and wrong value of b[0].


Refer to the comments.

Hope, this would help.
Give a like if you are satisfied.

ok sir, thanks a lot!