Stock Exchange Losses puzzle discussion

Feel free to send your feedback or ask some help here!


Hi, its strange, for this one, test5, I executed code on my local and I printed the expected result.
But on CodinGame, it seems either the script timed out before the end or the input was truncated.

I’am using javascript code.

Brute force approaches and simple optimizations time out even in C++, but with a clever trick I could complete in time using both languages I tried with, C++ and Python.


Yes, solution is to be clever… I succeed by using tricky optimisation…


I’m having some issues with this during submission. I have solved the problem, all of my tests pass, but when I submit it the first test fails. The first test during submission doesn’t seem to be much different than the first test that is provided while writing the solution. It seems to me that my solution is solving it correctly but that it’s being marked as incorrect. Has anyone else had this problem?

I get weird results for test5. It won’t pass and I can’t capture the entire input because there are so many numbers. All my other tests pass.

Can I get the input data for test5? All my tests pass except for this one and I can’t seem to figure out what is going wrong. My code seems correct to me. Alternately I can post my code if you can tell me if anything is wrong then I will go back to the drawing board. I don’t want to be told how to fix it I want to do that on my own…

Your code is too slow. I got exactly the same problem, and it took me some time to understand what was happening.

In short, you should find a solution in O(n), while the trivial solution (probably the first idea for most people) is in O(n^2).

It is pity that the error is not more explicit. First, the text of the problem should indicate clearly that the expected solution must be optimal. For most other problems, speed is never an issue, so I guess it is unexpected to fail due to a slow program. Secondly, the error should be much more explicit.


My code is O(n) and it runs to completion. I get this output on Test 5: Does this mean I am overflowing an int type?

Console output

Found: “-1073734”
Expected: “-1073730”

What are the two numbers in the input that make up the loss calculation?

What are the two numbers that make up the loss calculation?

You can’t overflow your int, because of the constraint 0 < v < 2^31. However, this output is exactly minimum - maximum of test 5 values so there’s something wrong in your algorithm.

Also, inputs and expected outputs for the IDE tests are given at the end of the puzzle statement.

I finally figured this one out. The problem was in my logic. What a clever puzzle.

first test get error even if i print real answer (Python)

Do you print it along with an end of line ?

I must be somehow fundamentally misunderstanding the requirements on this one. All 5 of the tests pass in the IDE, but when I submit, then grading test #1 fails:

Case (4 3 5 2 1 6) -> -4 (300 pts)

Looking at the data, it seems to me that my algorithm is working correctly. The largest loss in this dataset is from 5 (3rd entry) to 1 (5th entry). My program prints out “-4”, as shown. Is this not the correct answer?

  • dan
1 Like

Never mind. I figured out the problem through trial and error (lots of submissions). There must be an extraneous space in the input of the first validation test for that problem. I had been just doing a split on " " (single space) to get the input integers and then looping through the result, but that made me overflow my array (more than n results from the split operation).

Once I put some defensive logic into this part of my program, then I’m able to pass all the validation tests. Unfortunately, since I can’t get any debugging info from the validation tests, it’s hard for me to know exactly what about the input my program didn’t like.

  • dan

I found bag in this task (in first test) n==5 but array has 6 numbers

Wow. Was using LINQ’s TakeWhile and Last methods, everything was fine, but fifth was failing.

It forced me to think a bit what i get in those number ranges :smiley:

I agree with your assessment, @alexxx_was. I was wrong about extra spaces in the “vs” string. The first validation test has n=5. To test this theory, I submitted the following Java:

public static void main(String args[]) {
    Scanner in = new Scanner(;
    int n = in.nextInt();
    String vs = in.nextLine();

This code passes the first test only with “correct” output: “-4”. The test is providing invalid input.

  • dan