Horse Racing Duals puzzle discussion


Maybe your algorithm is not optimized enough?


Salut à tous! Je suis nouveau sur CG. Y’a des tests supplémentaires dans horse-racing, je ne sais pas comment les passer. Qcqn a une iddée?


Hi there !

Is someone using Kotlin for this puzzle ?
I passed all the tests except the ‘huge’ ones…

It seems that I can’t even store the int in array without timeout.

this code :

 for (i in 0 until N) {
        //val pi = input.nextInt()
        lesdadas+= input.nextInt()

without anything else make a timeout.
Is someone succeed in Kotlin?


I wanted to make a liste with the pi
I putted
10. liste.append(pi)

but it doesnt work, I got the message
SyntaxError: Non-ASCII character ‘\xc3’ in file on line 10, but no encoding declared; see for details

at not in a function on line 10

someone can explain ??


Did you try to use a non-ASCII character in Python 2?


what 's non ASCII character (sorry Im begginer)
Something like é, @ ??

No I didnt


Ok I found the problem. It was because I putted : " # We create a liste ".
I delete it and it worked.
But I dont understand why…


Nothing special in “# We create a liste” but maybe you printed an no-break space.


I’ve used lists instead of arrays and it worked. This is the only difference between your algo and mine.


Aaaaaaarg ! I just did it and it’s fine ! :slight_smile:
Thanks a lot !


Hello , can i get the validator test by private message too. I am facing the same error for “Shorter difference on the first 2 horses”. Thanks


Hi guys, just solved this in java. But found an odd misbehavior of the iteration through array list via for cycle.

See here :

import java.util.*;
import java.math.*;

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

    public static void main(String args[]) {
        Scanner in = new Scanner(;
        int N = in.nextInt();
        ArrayList<Integer> powers = new ArrayList<Integer>();
        for (int i = 0; i < N; i++) {
            int pi = in.nextInt();
        //sort numerically

        // go through array, remembering the smallest differnce
        int diff = -1, prev = -1;
         int curr = -1;      
        for (int i = 0; i < N-1; i++) {
 //       for (int curr : powers) {
           prev = powers.get(i);
           curr = powers.get(i+1);

    //first horse
    if (prev == -1) {
        prev = curr;
    //second horse
    if (diff == -1) {
        diff = curr - prev;
        prev = curr;
    } else
    // third and more ....
    if (curr - prev < diff) {
        diff = curr - prev;
        prev = curr;

    // Write an action using System.out.println()
    // To debug: System.err.println("Debug messages...");


When I try to use the iteration (commented out) via the for (item : array) construct, then the 3rd test fails by giving different result than it is supposed to. First 2 tests work normally.


Hello, I’m using C and I get time out at many horses and numerous horses.
I used Quicksort and single for loop comparison everything in it’s own function.


I’m using C# and I can’t do the last one of the many horses it says it takes to much time.
Using 2 for loops to search na array with the values.


Hi, I also used Quicksort and then a loop iterating over all sorted values to find the lowest difference. I failed the last test at first then I changed the way i choose my Pivot element to be the median of three values, for those values i chose index 0, index size - 1 and index size / 2 then it worked out for me. btw i was using c++



In JS, I obtain a difference in power that is less (30) than the one the test suggests (47). Is this a bug ?



Hi, I am desperately trying to understand why this code get the segmentation fault error when I test it. As a noob trying to learn, I d like to understand why, more than to find a workaround without first a comprehension of the error.

Thanks for any help!

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

int main()
    int N;
    vector <int> MyN(N);
    cin >> N; cin.ignore();
    for (int i = 0; i < N; i++) {
        int Pi;
        cin >> Pi; cin.ignore();
    sort(begin(MyN), end(MyN));
    int minSpace = 10000000;
    int currentSpace = `Preformatted text`0;
    for (int i = 0; i < N-1; i++)
        currentSpace = MyN[i+1]-MyN[i];
        if (currentSpace > minSpace)
    cout << minSpace << endl;
    //action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;



hey, why cant i use Arrey.sort?


maybe because it is “Array.sort” ?


Possibly because you initialize myN before you have read N, so the value of N is random and may exceed the allowed memory size.