Can't Get AC with JS even with optimized algorithm

The Solution

That is my solution, It’s having an complexity of O(nlogn) which is sufficient to pass this problem. But still I’m getting TLE.

I’m using bufferReader instead of Scanner as well.

Any idea what’s wrong here?

Given x and y, if (x < y) x%y = x else y%x < x
Thus, the fastest method is to not use sort. For any test case, just accept all input and store the minima. Print the minima for the answer at the end.

1 Like

Even if i comment out the sort, still TLE🤣

O(n) is also failing

O(n) solution

1 Like

First, even though you are getting TLE - am not sure your logic is correct. You cannot modulo them in the given order. This is the correct logic:

I dont know how to quickly process so many arrays of numbers into numbers in Javascript.

I know, i just commented out to see if " get rid of TLE " . I am wasn’t expecting a AC just a WA.

It’s weird because i don’t know how can simply using even a modulo operation on an array can give TLE🤦🏻‍♂️

1 Like

How you are using java functionality in your JS program ?

What java functionality?

TLE even with Math.min(). Surprising! :thinking:

1 Like

Exactly :rofl:i hope some expert JS programmer would enlighten me

1 Like

These …


We have to maximize the cost in the end.
The cost will be maximum only when we do modulo with a large number.

Eg:  if a > b then  a mod b < b mod a`

So we will have to find a permutation of the given numbers in such a way that we can do modulo with a large number each time.

So we can simply sort the sequence and get the cost by doing mod in the series.

Solution is in C++ though.

Thanks a lot bro🤣but i know how to solve this question obviously… Problem is how to solve using JavaScript… It’s giving me TLE

Sorry for the previous post actually I do not read your complete post
We can still solve the problem without using sort by getting the minimum element of the array.
I do not know how to use buffered reader so I have done it using scanner
You could modify it using reader class


Good job figuring it out. However, I wonder why this doesn’t work. I swear I have simply copied and pasted your code and clicked on ‘Submit’. Are you using the JS (Rhino 1.7.7) interpreter (or a compiler maybe, correct me if I’m wrong) ?

1 Like


AC =

These two are literally copy paste on same language, why the difference in verdict?

Edit = I just copy pasted @nikunj_1999 's solution, and I got TLE again ! :laughing:

Edit2 = It got accepted after I submitted 2-3 times lol :laughing: what’s happening?


Illuminati confirmed!


Yes, I am using JS Rhino