Horse Racing Duals puzzle discussion

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

Small challenge: solve it without sorting!

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);
}

}

Hi

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

horses[i+1]

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

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.

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.

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

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!

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

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

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);
}

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:

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.

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

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 !

[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]?

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

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.

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.

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