×

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

 0 #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>s[i]; sort(s,s+n); //for(i=0;i(s[i]-s[i-1])){ int temp=0; temp=diff; diff=temp; } } cout<

 1 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 ) 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. In your code the inner if block is not doing any useful work, only assigning diif to 0. if(diff>(s[i]-s[i-1])){      int temp=0;      temp=diff;      diff=temp; } 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. answered 30 Jan '15, 19:06 1.1k●3●13 accept rate: 36% Thanks @mediocoder , you helped me again :) :) (30 Jan '15, 21:00) You're welcome, glad to help. :-D (30 Jan '15, 21:08)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,738
×1,918
×1,491
×856
×12

question asked: 30 Jan '15, 18:38

question was seen: 877 times

last updated: 30 Jan '15, 21:08