 # Horse Racing Duals puzzle discussion

#356

and yet the command wont work
error: cannot find symbol

``````    Array.sort(horses);
``````

edit: Arrays.sort
damn
tnx(;

#357

Im trying to understand why I only have 90% and I can’t pass the Horses in disorder test but I can’t figure it why? I’m trying to learn c++ pls help!

Thanks!

``````int main()
{
int N;
cin >> N; cin.ignore();

vector<int> PI;
vector<int> Diff;
int D = -1;
int previous = -1;

for (int i = 0; i < N; i++) {
int Pi;
cin >> Pi; cin.ignore();

//cerr << "Pi = " << Pi << endl;

PI.push_back(Pi);
}

sort(PI.begin(), PI.end(), greater<int>());

for(int k = 0; k < N; k++)
{

int tmp;

if(previous == -1)
{
previous = PI[k];
}
else
{

tmp = previous - PI[k];

if(D == 1 || tmp == 1)
{
D = 1;
break;
}

else if(D == -1 || D > tmp)
{
D = tmp;
}

previous = PI[k];
}

}

// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;

cout << D << endl;
}``````

#358

I can’t find where the problem is. Can you help me, please?
for (i = 0; i < N; ++i) {

``````    cin >> P[i];
// cin.ignore();
cout <<"power " << P[i]<<endl;

}

for (i = 0; i < N; ++i) {
d = P[i + 1] - P[i];
cout <<"d  "<< d << endl;
min=0;
if(P[i] < min) {
min = P[i];
}
}``````

#359

You output everything but the answer
If you need to debug you should use cerr instead of cout

#360

Thank you for answer ! )

#361

I’m using qsort in c for this but it’s not working which is odd… Why though?

Edit: It worked!! That damn \n kept me thinking for an hour -_-
got 100% finally <3

#362

Hi everyone,

FWIW, in C,
I had timeout in the many horses test
whether I did an insertion sort (embedded in the scanf loop or after) or a quicksort.
I passed 100% only by using qsort.

Have a nice time #363

Salut, je valide ce test à 90% quand je soumets mon résultat alors que les tests sont valides. Quand pensez-vous ?

``````public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
TreeSet<Integer> tree = new TreeSet<Integer>();
for (int i = 0; i < N; i++) {
int pi = in.nextInt();
}
Iterator treePuisaance = tree.iterator();

int difResult = 0;
int valeurPrecedente = (int)treePuisaance.next();
while(treePuisaance.hasNext()){

int valeurCourante = (int)treePuisaance.next();
int difCourante = valeurCourante - valeurPrecedente;
if(difResult == 0 || difCourante < difResult){
difResult = difCourante;
}
valeurPrecedente = valeurCourante;
}

System.out.println(difResult);
}``````

#364

Something is wrong with C# here. I tested it and its returning the wrong number although the console is proving I am feeding it the correct answer.

``````using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution
{
static void Main(string[] args)
{
List<int> list = new List<int>();

for (int i = 0; i < N; i++)
{
Console.WriteLine(pi);
}

Console.Error.WriteLine();

list.Sort();
int diff = 10000000;

foreach(var v in list)
Console.Error.WriteLine(v);

Console.Error.WriteLine();

for (int i = 1; i < N; i++)
{
int z = list[i] - list[i - 1];

if (z < diff)
diff = z;
}

// Write an action using Console.WriteLine()
// To debug: Console.Error.WriteLine("Debug messages...");

Console.Error.WriteLine(diff);
Console.Error.WriteLine();

Console.WriteLine(diff);
}
}```

``````

5

8

9

5

8

9

1

1

``````

#365

#366

Check which of your printing are on the output console and which are on the error console.

#367

Everything is on the error console, and the var passed to that to verify its the correct answer is the same that is passed into WriteLine.

#368

I thought it was pretty obvious from the pic and the code that you can tell what is what in which order. I literally have the same exact solution in C++ (except for cout) without issue.

#369

So I’m not sure why I get different results when the logic is identical but just a different language.

``````#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/

int main()
{
int N;
cin >> N;
cin.ignore();
vector<int> vec;

for (int i = 0; i < N; i++)
{
int Pi;
cin >> Pi;
cin.ignore();
vec.push_back(Pi);
}

sort(vec.begin(), vec.end());
int diff = 10000000;

for (int i = 1; i < vec.size(); ++i)
{
int z = vec[i] - vec[i - 1];

if (z < diff)
diff = z;
}
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;

cout << diff << endl;
}`````````

#370

Your c++ solution and your c# solution implements the same algorithm, but they do not provide the same output log and error log.

#371

Why when i use .sort() in JavaScript, this doesn’t count disorder success ? I finish only with 90%…

#372

When i use sort = > 90%
“Horses in disorder” not ok…
I don’t understand.

#373

With sort, you have less operations to do because you have no more to compare each horse with all others.

But if your sort does not take care that values are number and not string it is an epic fail …

I don’t know how you coded but you should check that because it is classic error

#374

In javascript, pi is a number. I create an array with pi, and i sort() this for compare latter. I sort an array of number.
If i try another solution, my script are too long.

#375

yes, definitely.
precise and perfect hint