Stock Exchange Losses puzzle discussion

I have solved this in JavaScript fine however in Dart it is timing out in Test #5 even when running the below script to get the numbers it times out - this has to be an error with Dart?

void main() {
  readLineSync();
  readLineSync().split(' ').forEach((value) {});
  
  print(-1073730311);
}

Test case 5 shows a timeout issue even with the default code.
@TwoSteps Could you please take a look? Thanks.

1 Like

Thanks for your idea and I settle this puzzle by it using cpp. :smiling_face_with_three_hearts:

I’ve settled this puzzle with cpp . If you [nope].

Does your code contain any lines to print to error stream? If so, your code may pass if you comment out those lines.

i get it,thanks

in test 5 i keep getting
Found: -1073706325
Expected: -1073730311
and i have no idea why :confused: (using c++)

4246 - 1073734557 = -1073730311
I think you may consider checking how you get -1073706325 and why you have skipped the expected answer by adding some code to print to the error stream.

Well, @logicow is right.
You may need a linear algorithm like greedy algorithm, which works on this task.

This should be in easy.

Hi i wrote a O(n) algoritm to solve this puzzle. It pass all tests but test 5.
My code gives different result than expected.
I should be missing something but cant figure it out.
Any idea?

I found my mistake thanks …

1 Like

Great, you are fast ! We didn’t have even the time to help. :slight_smile:

1 Like

Is an error in test5 like
Failure
Found: -1071590351
Expected: -1073730311
means that there is actually a timeout happening behind the scenes or is there a hidden trap in test5’s dataset?
All the other tests are passing, and I optimized my code to run in a single loop reading the inputs with just 5 variables and zero data structures, so not too sure where to go from there optimization-wise.
(Using Java if it makes a difference)

I found this problem enjoyable. when I ran my program it passed every test case that was presented, except for the particular case where the input is [n1, n2, … 2, 2, 1] or [n1, n2, … 3, 2, 1] in which none of the test cases handled properly.

Hi Guys,
i have a problem with the last example (6), i need help.

import sys
import math


n,data,result,= int(input()),[],0
for i in input().split():
    v = int(i)
    data.append(v)
beforeD,maxD,minD= data[0],max(data),min(data)
if data.index(data[0]) != data.index(maxD):
    if data.index(minD) > data.index(maxD):
        result = (maxD-minD)*-1
        print(result)
    else:
        newData = data[:data.index(minD)+1]
        maxD = max(newData)
        result = (maxD-minD)*-1
        print(result)
else:
        result = (maxD-minD)*-1
        print(result)

thanks for your help

In case you do not know, you may look up what the input is by clicking the button above the PLAY TESTCASE button. You should be able to debug by doing a manual calculation and comparing the calculation process with that done in your code.

Agree with you , problem with the input and expect ouput: n= 6

a =[3,2,4,2,1,5]

#ouput = -3 I should be -2

The puzzle statement contains a graph which shows why the answer is -3. Please take a look.

[mod edit: please avoid posting code on the forum]
I couldn’t pass the 5th test how can i optimized my code enough to pass