Defibrillators puzzle discussion


I have solved this puzzle both in JS and Java.
For Java i got 100% validation with 3rd test case being wrong, but that’s ok.
And my JS solution is not passing though the math looks the same…

I was wondering if anyone can help me spot an error.
I know that in Java you have to use doubles as the Math functions take doubles in when calculating.
In Js you dont have that option and only are left to parse to float.

Here is my Java to JS comparison:

 private static double getDistance(double longitudeA,double longitudeB,double latitudeA,double latitudeB){
        Double x= (longitudeB-longitudeA)*Math.cos((latitudeA+latitudeB)/2);
        Double y=latitudeB-latitudeA;
        Double dist =Math.sqrt(Math.pow(x, 2)+Math.pow(y,2))*6371;
        return dist;
function getDist(cLon,cLat,gLon,gLat){
    const x =(gLon-cLon)*Math.cos((cLat+gLat)/2);
    const y =gLat-cLat;
    return Math.sqrt(x*x+y*y)*6371;

Transforming degrees to radians:

      Double LON = Math.toRadians(Double.parseDouble(",",".")));
      Double LAT = Math.toRadians(Double.parseDouble(",",".")))
      //for array of defibs
      Double longitude=Math.toRadians(Double.parseDouble(
                    DEFIB.split(";")[DEFIB.split(";").length-2].replace(",", ".")
      Double latitude=Math.toRadians(Double.parseDouble(
                    DEFIB.split(";")[DEFIB.split(";").length-1].replace(",", ".")
      const LON = parseFloat(readline().replace(',','.'))*(Math.PI/180);
      const LAT = parseFloat(readline().replace(',','.'))*(Math.PI/180);
      //for array of defigs

Maybe you can try writing snippets (separate from the puzzle solution code), and passing the same input to both codes to determine which lines produce different results?

I wrote such code. I pass tests in IDE but after submiting it fails at Complete file 2 test.
Any ideas what I’m doing wrong? It’s confusing.

[Mod edit: Please avoid posting codes on the forum.]

The most probable issue is not converting the input (in degrees) to radians.

If there is still an issue, another probable cause is timeout. Try splitting only once per loop and avoiding sorting (you have another way to determine the minimum).

Please avoid posting codes on the forum. Try describing your approach first instead.

This fixed my code, thanks!

thank you for this! was getting the first test wrong even though all test cases passed, after changing to this formula i was able to get 100%

Thank you!!! I wasted hours trying to figure out why the other formula wasn’t working.

As soon as I switched the distance calculation to use this formula instead, all of my cases passed.