We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here, there are 499 records in station table. This means that the median is 250th record if the tabled sorted either asc or desc. In the query of alexseedkou for every S.LAT_N of S table he is creating two tables which are one for number of LAT_N values less than S.LAT_N and one for number of LAT_N values greater than S.LAT_N. Later he is comparing these two tables and if they are matched then he is selecting that S.LAT_N value. When he check for 250th records there are 249 values less than 250th record and there 249 records greater than 250th record. As you see, there are 249 records in both tables. So this is our answer. For every other record there are different number of records in two tables created for comparison. So, only 250th record meets the condition. But in this algorithm there is a flaw for tables whose number of records are even. Lets say there are 500 records in station table. In this case median of the station table is average of 250th and 251st records if table is sorted according to LAT_N. But there is no such record in station table. He should find 250th and 251st records and calculate median. So, this solution won't work for every table.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Weather Observation Station 20
You are viewing a single comment's thread. Return to all comments →
Here, there are 499 records in station table. This means that the median is 250th record if the tabled sorted either asc or desc. In the query of alexseedkou for every S.LAT_N of S table he is creating two tables which are one for number of LAT_N values less than S.LAT_N and one for number of LAT_N values greater than S.LAT_N. Later he is comparing these two tables and if they are matched then he is selecting that S.LAT_N value. When he check for 250th records there are 249 values less than 250th record and there 249 records greater than 250th record. As you see, there are 249 records in both tables. So this is our answer. For every other record there are different number of records in two tables created for comparison. So, only 250th record meets the condition. But in this algorithm there is a flaw for tables whose number of records are even. Lets say there are 500 records in station table. In this case median of the station table is average of 250th and 251st records if table is sorted according to LAT_N. But there is no such record in station table. He should find 250th and 251st records and calculate median. So, this solution won't work for every table.