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.
It's very similar to one of the top comments here, but it should work in both cases (number of rows even or odd).
It takes the average of rows for which the number of smaller values and the number of greater ones do not differ by more than 1. In case of even number of rows, for the two values in the "middle" this difference equals 1, so the resulting median will be their avarage. In case of odd number of rows, it will do the job as well.
However, this solution is still not universal - if there are more than one rows containing the median value, this will still fail - we should then find the minimum value of that expression in my query, so it might complicate things a bit more.
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 →
@ankitraheja Here's what I came up with.
It's very similar to one of the top comments here, but it should work in both cases (number of rows even or odd).
It takes the average of rows for which the number of smaller values and the number of greater ones do not differ by more than 1. In case of even number of rows, for the two values in the "middle" this difference equals 1, so the resulting median will be their avarage. In case of odd number of rows, it will do the job as well.
However, this solution is still not universal - if there are more than one rows containing the median value, this will still fail - we should then find the minimum value of that expression in my query, so it might complicate things a bit more.