ANEO Sponsored Puzzle discussion


With the expression (1/180) you are dividing two integers, and the result will also be an integer. (1/180) will evaluate to an integer value 0.

Replace it with (1.0/180.0) and it will probably work.


how can i ask my question
i have a doubt in this puzzle
if speed is already given what is needed to be done


i don’t understand anything


the puzzle says to output the maximum speed but the outputs are the same or less than the riginal speed pls explain


The goal is to find the maximum speed (off speeding) that will allow you to cross all the traffic lights to green.

At maximum speed, you’ll probably pass a traffic light while it’s red. That’s why you need to slow down so you pass all traffic lights to green.


Dear all,

I passed all the tests except the last one with 100 lamps.
In this last case for the supposedly “winner speed” given in the solution my method gives only 86 green lamps out of 100. I wonder why?
I convert the initial int value of distance into double type km and the duration in double type hour value.
My method in Java:
The “outer” loop, which is already given, is that of the lamps.
I create an array speed[] which represents all the int speeds from 1 to max. speed (all the values of this array are 0 at the beginning, only the indexes “show” the speed, not their value), and with the “inner” loop I go through all the speeds for each lamp. If the lamp is green for the speed “z” then speed[z] increases with 1.
Whether a lamp is green upon the arrival of the car, I calculate like this.

  1. If speed X duration ≥ distance, then its ok, green.
  2. z is an even integer increasing in a loop (2, 4, 6, etc). If z X speed X duration < distance and distance < (z+1) X speed X duration, also then its green.

At the end I search for the biggest value in speed[] starting from max speed going backward to the lowest.
Where can there be a mistake?


I am just about to begin solving this challenge. At first I thought I understood the problem but printing out some test cases got me very confused. Perhaps I am a little thick today, I am finding it hard to understand the problem even though I am a native english speaker.

In test case 1, distance to the traffic light is 200m and the light switches every 15 sec. The answer is 50 km/hr because anything less than this it will be unable to cross the light before it tuns red (by the way, shouldn’t it be 48 km/hr?).

But in test case 2, distance to traffic light remains the same but the period of light switching reduces to 10 sec. Shouldn’t the speed then be greater than 50km/hr? How is it the answer is 36? Kindly help.


Because the maximum allowed speed for test case 2 = 50 => can not cross the light in 1st duration (green) => coss the light in [2k,(2k+1)] * duration
speed = 36km/h = 10m/s => After 10s green + 10s red => 10 *20 = 200 = distance. But i think 20th second is still in red duration. I passed this test but when i submit it failed
sorry for my english


You don’t have to pass the light before it turns red. You have to pass the light when it is green.

In other words, it is allowed for the light to turn red, as long as it is green again when you pass it.


aah, I get it now. Thanks very much for clearing that up. I went back to read the problem statement again with this knowledge and it still wasn’t clear. :stuck_out_tongue:


I found that the inequality below is able to check whether the light (at distance ‘d’,duration ‘t’) will be green or red while moving at the velocity (‘v’).

The light will be green if:

    d/v - t [ floor(d/vt) + mod( floor(d/vt) ,2 ) ] >= 0

The light will be red

  • I am actually trying to find the nearest even multiple of ‘t’ to ‘d/v’.


I suspect most of the problems people run into is this rule “You can not cross a traffic light the second it turns red !” You have to round such that this holds true in your math. That is you can hit a light just as it turns green, but you must hit a light before it turns read. Use floor and ceil wisely.


Finally, I got 100% score.

I found that using floor(d/(v*5/18)) works very well.

Thanks ANEO for this interesting puzzle. :slightly_smiling_face:


I’m annoyed by this puzzle…

 * Returns time (in seconds) to travel the given distance and the given speed. 
 * @param distance (meter)
 * @param speedInkmPerHour (km/h)
 * @return time in second to travel the given distance.
private int getDuration(int distance, int speedInkmPerHour);

Different implementations and result for the tests :

  • (int) Math.round(distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 100% Given tests / 80% Validator tests
  • (int) Math.floor(distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 80% Given tests / 90% Validator tests
  • (int) Math.ceil(distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 80% Given tests / 80% Validator tests
  • (int) (distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 80% Given tests / 90% Validator tests

or even

  • (int) Math.round(distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 100% Given tests / 80% Validator tests
  • (int) Math.floor(distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 80% Given tests / 90% Validator tests
  • (int) Math.ceil(distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 80% Given tests / 80% Validator tests
  • (int) (distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 80% Given tests / 90% Validator tests


It is annoying. You must use both floor and ceil, never round. However, you haven’t said where you are using these rounding techniques. It matters. You can arrive at a light just when it turns green, however, you may not arrive at a light just as it turns red.


Thank you. I will try these.

I use the following method with the time given by the method “getDuration(int distance, int speedInKmPerHour)” :

 * Returns if time is when light is green. <br/>
 * Exclude instant when green -> red <br/>
 * Include instant when red -> green.
 * @param lightPeriod (in seconds) green -> red -> green -> red
 * @param time (in seconds)
 * @return
private boolean isGreen(int lightPeriod, int time)
	if(time < lightPeriod)
		return true; // Pass before green -> red, OK
	if(time == lightPeriod)
		return false; // Pass when green -> red, not OK
	if(time < lightPeriod*2)
		return false; // Pass before red -> green not OK
	return isGreen(lightPeriod, time-(lightPeriod*2));

But maybe is should try another approach.


IDK guys why are you going in such complicated matters on a problem which can be easily solved with a do while :))


Yes I don’t see why 89 is not accepted, maybe a rounding problem because with floating numbers, 24.68 is the answer


Test 2: distance 200m duration 10s with expected answer 36km/h. This would get the car at 100m in 10 second the light will go red and then the car will get to 200m at 20s when the light will be green. That is ok but what if we run at 72 or 73 or 74km/h and pass the light at the first 10 seconds? Actually you can set many velocities to pass the light since upper limit is 200km/h. In 200km/h -> 55m/s pass the light in less than 4 seconds. Discouraging


In Test 2, 200 is the distance, not the upper limit.
The upper limit is set to 50 km/h.