You are not logged in. Please login at www.codechef.com to post your questions!

×

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

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

asked 30 Jan '15, 18:38

shubham99's gravatar image

2★shubham99
2403932
accept rate: 5%


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[i]-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.

link

answered 30 Jan '15, 19:06

mediocoder's gravatar image

3★mediocoder
1.1k313
accept rate: 36%

edited 30 Jan '15, 19:24

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

(30 Jan '15, 21:00) shubham992★

You're welcome, glad to help. :-D

(30 Jan '15, 21:08) mediocoder3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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