HORSES : Wrong answer according to judge but working correctly on my compiler

#include
#include
using namespace std;

int main(){
int t,n,i,diff=0,s[10001];
cin>>t;
while(t--){
	cin>>n;
	for(i=0;i<n;i++)
	cin>>s*;
	sort(s,s+n);
	//for(i=0;i<n;i++)
	//cout<<s*;
	for(int i=1;i<n;i++){
		diff=s[1]-s[0];
		if(diff>(s*-s[i-1])){
			int temp=0;
			temp=diff;
			diff=temp;
		}
	}
	cout<<diff<<endl;
}
return 0;
}

Your code gives wrong answer for this case

4 9 1 15 13

There are two mistakes ( three if you use the same loop iteration )

  1. Main mistake :You are assigning the value of diff to s[1]-s[0] in the statement diff=s[1]-s[0]; at every iteration.
  2. In your code the inner if block is not doing any useful work, only assigning diif to 0.

    if(diff>(s*-s[i-1])){

         int temp=0;

         temp=diff;

         diff=temp;

    }
  3. Change (not necessarily, depends on your implementation ) the for loop starting and ending values.

Put the statement diff=s[1]-s[0]; before the for loop and change the inner if block.

Check your same code Accepted with the above modifications by me here.

For memory optimization you can use array size of 5001 as the constraint is 2 ≤ N ≤ 5000. You are using large array size which requires more memory.

1 Like

Thanks @mediocoder , you helped me again :slight_smile: :slight_smile:

You’re welcome, glad to help. :smiley: