Showing segmentation fault

Getting error /bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

code:-

#include
#include <bits/stdc++.h>
#include
using namespace std;
int main()
{
int t, n, m;
cin >> t;
while(t >0)
{
cin >> n;
int a[n]={0};
for(int i=0; i<=n-1; i++)
{
cin >> a[i];
}
sort(a,a+n);
cin >> m;
int difference =INT_MAX;
for(int j=0; j<=n-2; j++)
{
for(int k=j+1; j<=n-1; k++)
{
if(a[j] + a[k] == m)
{
if(a[k] - a[j] < difference)
{
cout << "Deepak should buy roses whose prices are "<< a[j] << " and " << a[k] << “.”<< endl;
difference = a[k] - a[j];
}
}
}
}
t–;
cout << endl;
}
return 0;
}

@vedantmendiratta_2b986865346635fc its not giving an answer because thats not giving the pair which has minimum difference bcz when you print pair after that u also try minimize difference so you have to store difference with its pair values after that you have to find minimum pair with smallest difference.

when i ran the code on codeblocks, it ran only once, (even if their are multiple test cases), giving the correct output, and then it shows an error.

@vedantmendiratta_2b986865346635fc in this u have given 2 tc but input is 1
i will check your code see what happens

@vedantmendiratta_2b986865346635fc bcz when i run your code it does not even run not giving anything

yes, even on the coding blocks ide it does not run even once

@vedantmendiratta_2b986865346635fc i will check your code

1 Like

@vedantmendiratta_2b986865346635fc check this u have done mistake according to your question
https://ide.codingblocks.com/s/646907 check this

1 Like

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.