https://www.codechef.com/problems/HORSES

https://www.codechef.com/problems/HORSES

Can someone plz help
Ive compared with the correct submissions and all tested cases have same answers but it shows wrong answer
#include

#include<math.h>

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int T,t,i,k,mn,md,l;

 

cin>>T ;

for(t=0;t<T;t++)

{

        cin>>i;

           int S[i];

for(k=0;k<i;k++)

{

 cin>>S[k];

}

for (k=0;k<i;k++)

{

 for (l=0;l<i;l++){

     if(k==l)

     {continue;}

       mn=S[k]-S[l];

if(mn<0)

{

    mn=-1*mn;

}

if(k==0)

{

    md=mn;

}

if((mn<md))

{

    md=mn;

}

     

 }

}

cout<<md;

}

return 0;

}

Firstly, fix this warning:

[simon@simon-laptop][20:24:57]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling terminator_v10-HORSES.cpp
+ g++ -std=c++14 terminator_v10-HORSES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
terminator_v10-HORSES.cpp: In function ‘int main()’:
terminator_v10-HORSES.cpp:75:5: warning: ‘md’ may be used uninitialized in this function [-Wmaybe-uninitialized]
 cout<<md;
 ~~~~^~~~
+ set +x
Successful

Secondly, fix it so that it works for the test input:

2
5
4 9 1 32 13
5
4 9 1 32 13

There are other problems with it, though :slight_smile:

Edit:

Actually, just getting that testcase working might be everything, and that warning might be a red herring - the code is hard to read :slight_smile:

Thank you for replying , it doesnt show any warning in mine , you may have copied it wrong .
And the input format is same

The correct answer is 3 and its correct in mine

Please provide a link to your submission.

1 Like

https://www.codechef.com/viewsolution/48788556

Im kinda new to the discussion forum so plz guide me accordingly , thank u

Thanks!

[simon@simon-laptop][08:15:09]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling terminator_v10-HORSES.cpp
+ g++ -std=c++14 terminator_v10-HORSES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
terminator_v10-HORSES.cpp: In function ‘int main()’:
terminator_v10-HORSES.cpp:46:6: warning: ‘md’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  cout<<md;
  ~~~~^~~~
+ set +x
Successful
[simon@simon-laptop][08:15:14]
[~/devel/hackerrank/otherpeoples]>echo "2                                                       
5
4 9 1 32 13
5
4 9 1 32 13" | ./a.out
33

See the issue?

1 Like

oh okay I get that , Ill fix it

1 Like

Thank u so much it worked :sob: :sob:

1 Like