-2

I'm currently trying to solve the horse-racing game on Codingame. The problem statement is as follows:

Input

Line 1: Number N of horses

The N following lines: the strength Pi of each horse. Pi is an integer.

Output

The difference D between the two closest strengths. D is an integer greater than or equal to 0.

I'm using a std::set, and my algorithm is quite easy but for some reason it fails 2/8 of the submission validators. I don't have access to the actual test data, but according to the test names, the code below fails for

  • all horses same strength
  • horses in disorder

Why does my code fail on that input?

#include <iostream>
#include <set>

using namespace std;

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

    set<int> power;

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

    int minDiff = 10000000;

    auto i = power.begin();

    while (i != power.end())
    {
        minDiff = min(minDiff, abs(*i - *(++i)));
    }
    
    cout << minDiff << endl;
}
Community
  • 1
  • 1
A.LeBreton
  • 75
  • 10
  • 1
    Questions seeking debugging help (‘**why isn't this code working?**’) must include the desired behaviour, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Biffen Dec 26 '16 at 10:06
  • 2
    *"I'm kind of stuck on the Horse-racing one,"* Not everyone knows what that "codingame" is, neither do they know what "horse-racing" should be. Show your output and the expected one, or at least add enough of the problem description so that one can actually help you. – Zeta Dec 26 '16 at 10:07
  • 1
    Please don't add "EDIT:" to your post Everyone can see your posts [history](https://stackoverflow.com/posts/41329526/revisions). Just improve the overall quality of your question instead, without adding additional noise. – Zeta Dec 26 '16 at 10:09
  • And last, but not least, make sure that you include __all__ the code. – Zeta Dec 26 '16 at 10:20
  • Plase also see [why not to use `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Quentin Dec 26 '16 at 10:30
  • `*i - *(++i)` is undefined so that program can do anything. – molbdnilo Dec 26 '16 at 10:34
  • if power.size() != n you can safely print 0. – user1438832 Dec 26 '16 at 10:35
  • @Zeta Thanks, I'll try to apply that to my future posts. – A.LeBreton Dec 26 '16 at 10:36
  • @user1438832 Yes, that's what I've done to fix my problem. – A.LeBreton Dec 26 '16 at 10:38
  • 1
    I don't really understand why you chose to use a `std::set` in first place - it kills horses with the same strength (which you don't want) and is less efficient than using a plain vector and sorting it after the input (all in all they do have the same algorithmic complexity, but the `set` has bigger constants). – Matteo Italia Dec 26 '16 at 10:46

1 Answers1

1

Your solution fails if any two horses have the same strength:

std::set<int> horses;
horses.insert(1);
horses.insert(2);
horses.insert(1);

// your algorithm returns 1, but it should be 0

Also, you have undefined behaviour. What happens if ++i is power.end() here?

minDiff = std::min(minDiff, abs(*i - *(++i)));

Use two iterators instead, and check for the special case where you have only one kind of horse if you want to use std::set.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 2
    The special case is not just every horse having the same strength, it's any two horses with the same strength. If three horses have strengths 1, 1, 2, I think the OP needs to output 1-1=0. –  Dec 26 '16 at 10:28
  • @hvd: actually, that could be an early exit - before inserting the new horse check if it's already there, in that case you can quit immediately printing 0. – Matteo Italia Dec 26 '16 at 10:51