Horse Racing Duals puzzle discussion


#221

Java has a built-in sort function for integers, just use that.


#222

Small challenge: solve it without sorting!


#223

Hi everyone!! I’m a beginner programmer and curently I’m stuck in my solution. It’s writtent in C#. I have this error

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Solution.Main on line 29

Can anyone can help me why it shows up:(

class Solution
{
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine());
int[] horses = new int[N];
for (int i = 0; i < N; i++)
{
horses[i] = int.Parse(Console.ReadLine());
}
Array.Sort(horses);
int minDifference = horses[1] - horses[0];
int currentDifference =0;
for ( int i = 0; i< horses.Length; i++)
{

        currentDifference = horses[i+1] - horses[i]; 
         if (minDifference > currentDifference)
            {
                minDifference = currentDifference; 
                }           
         }

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

    Console.WriteLine(minDifference);
}

}


#224

Hi

for ( int i = 0; i< horses.Length; i++)

horses[i+1]

i+1 is outside for the last interation of the loop.


#225

First, when I’ve submitted my code, they say that my algorithm fails with tie horses.
Ok, so I’ve managed to correct it and I’ve entered tie horses in the personalized test and failed.
Then I’ve submitted the algorithm anyway and the tie horses case is success.


#226

Anyway I just want to wish good luck to those who haven’t managed to resolve the puzzle yet. :slight_smile:
A little advice : It’s not because simplest solution isn’t the best that you’ll have to search way too far.


#228

can you tell me how to do it please? i’m a newbie of java and i can’t pass this


#229

Hello everyone!
I’m trying to resolve this puzzle in C++, but I have some problems.
In summary, the output of my code doesn’t match with the output of the test case.
I think that the following image summarizes a little bit better the problem:

Thanks in advance!


#230

Use cerr to display debug ouput like “The difference between…”. Use cout only for the expected answer (1)


#231

I resolved :slight_smile:
That was the problem :+1:
Now, I have to understand the fails in the validators panel !


#232

Hi, I have found a little issue, I hope i am mistaking , when I’ve printed the numbers for input I have found out that the first pw data is missing. This issue is in JAVA. I have manage to pass 91% of validation.
Example of error data

input: 3 5 8 9
output: 5 8 9

code used:
int N = in.nextInt();
for (int i = 0; i < N; i++) {
int pi = in.nextInt();
System.err.println(pi);
}


#233

Hi,
I have realized the bash code thanks for the previous replies which have helped me.
To help you to do it, here are some reminders and some tricks :

The best way to assign a value is ((diff=$x- $y)) instead of diff=expr $x - $y
To get an absolute value, you just have to do it : abs=${diff#-} instead of any if instruction.

Good luck. :heart:


#234

Well, just got a bash achievement, took me a while.

The trick is, that if you extract (i+1)th element from array before (i)th element, it somehow takes extremely more time, so the program needs more time to work than given time limit.


#235

Hello,

I’m coding using python, and I’m stuck at 90%, the test number 6 - horses in disorder - does not pass, while the same test in the IDE passes.
I read the hint but I don’t know what to perform to optimize the algorithm. When I try to add some test the code become to slow with a lot of horses. My code is after the post

Any help appreciated to help me perform the 100%.

Thanks

import sys
import math

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

numbers = []
difference = None

temp = int(raw_input())
if 1 < temp < 100000:
    n = temp

for i in xrange(n):
        pi = int(raw_input())
        if 0 < pi <= 10000000:
            numbers.append(pi)

del(n)
# Write an action using print
# To debug: print >> sys.stderr, "Debug messages..."

numbers.sort()

for i,n in enumerate(numbers):
        difference_current = abs(numbers[i] - numbers[i-1])
        if not difference or (difference_current < difference):
            difference = difference_current

print difference

#236

Bonjour tout le monde !

Je suis vraiment embêté avec le test de “Chevaux dans le désordre” aussi. ça passe parfaitement coté IDE mais le validateur me rejette et comme il n’est pas verbeux je ne sais pas si le problème vient du fait que les chevaux ne sont pas triés ou que le temps de traitement est trop long (sachant que je passe les tests “Nombreux chevaux”).

J’ai testé avec la fonction sort (en JS), tri fusion, tri rapide, tri par insertion à l’initialisation du tableau, mais rien n’y fait je suis toujours bloqué sur ce test coté validation (il passe toujours coté IDE)

En vous remerciant !


#237
[quote="baptistemm, post:235, topic:38"]
    for i,n in enumerate(numbers):
        difference_current = abs(numbers[i] - numbers[i-1])
[/quote]

what will be your first i? what do you hold in numbers[first i - 1]?


#238

Bonjour,
J’échoue au deuxième test de validation (plus petite dirfférence sur les deux premiers chevaux), et je n’arrive pas à voir pourquoi.
Est-ce qu’il y a un moyen de voir les input output de ces tests?
Merci


#239

Est-ce qu’il y a un moyen de voir les input output de ces tests?

si tu veux voir du debug, le plus simple comme détaillé dans le code c’est d’écrire sur la stderr.


#240

Hello bvs23bkv33

actually I surrounded the instruction with a condition if i>0:, I don’t know why it disappeared. However even when I had back the condition the test #6 still fails.


#243

Dear fellow Coders!

I really need your help with this puzzle. Logically, my code would work perfectly for all test cases, but for some reason it always prnts out a negative value!

My code is here.

By the way, I am using Python 3

I would really appreciate for any ideas on why my code is wrong