problem in output of horses question in easy tab most successful submissions.

i have been trying out this problem since a number of days. can someone please point out what exactly is my runtime error in the code. its the horses question in easy>most successful submissions.

int main()
int t,i,s[10],j,temp=0,result,n;
return 0;

You should format your code before posting it.


You forgot to include testcases statement, ie. while(t–) or for(int j=0; j<t;j++).

Can you please post the link to the question? I cannot find it, and at top or bottom 20. And easy section is too big :confused:

There are many things wrong with the code.

  1. As chan said, you forgot the statement while(t–}{} . You will be given T samples for which you will have to give T outputs. Since T was one this time, you didnt have problem. But had T been 2 or more, you will be solving only 1 out of T cases.
  2. “for(j=i+1;j<5;j++)” N will not be 5 every time. It can goto 5000. This loop will fetch you WA. MAke it “j<n-1” in condition part!!
  3. I see you used sorting. even after sorting, you cannot assure the result will be s[0]-s[1]. A proof is given below-

Lets say array is 40,2,59,3,45,2,4,20. After sorting in your order, it will be {59,45,40,20,4,2,2}. You said ans is s[0]-s[1] = 14. But in the end the two ‘2’ will give even less difference of 0.

You will need to check EVERY ADJACENT element for AC, isntead of just first two elements. After sorting we can give strong guarantees for sum (eg- In descending order, we can say that s[0]+s[1] > s[5]+s[6] and so on. BUT, we cannot say that s[0]-s[1]>s[5]-s[6] as difference, we cant say for sure)

Fixing this all should give you an AC i hope.