racing horses,code shows run time error...plz help in recognizing what is wron with the code

include

using namespace std;
long s[5000];
long int n;
main()
{
int t,x,a=0,i,m,j,min[10];

cin>>t;
if(t>=1 && t<=10)
{
for(x=0;x<t;x++)
{
cin>>n;

for(i=0;i<n;i++)
cin>>s[i];
if(s[0]<s[1])
min[a]=s[1]-s[0];
else
min[a]=s[0]-s[1];
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(s[i]>s[j])
m=s[i]-s[j];
if(m<min[a] && m>0)
min[a]=m;
}

}
a++;
}
for(i=0;i<t;i++)
cout<<min[i];
}

}

1 Like

Hello,

I guess you are new to Codechef community.
could please put the link of the problem and submitted the solution, which can help us to understand that what problem is.

otherwise, how could anyone help you?

happy coding

Your approach is correct. It is not fast enough. You need to optimize your code by thinking of a faster algorithm.

***** [ Hint: Your approach takes T * N * N time. There is a way to solve it in T * N * lg N time. Think of sorting the initial array O(N lg N) and then comparing the adjacent terms O(N) ] *****

1 Like

question: https://www.codechef.com/problems/HORSES
solution: https://www.codechef.com/viewsolution/11537188