Given a matrix (n x n) of 1 and 0, where 1 represent land and 0 represent water. How can I find the median of the area of the lands in the most efficient way?
For Example:
1 1 0 0 0
1 0 0 1 1
1 0 1 0 0
There are three islands, the area of them [1,2,4] and the median is 2
An island can be consist of continuous non-diagonal cells which contain 1: For example:
1 0 1
0 1 0
this matrix contains three islands of areas [1,1,1]
My solution is finding recursively the areas and then sort them to find the median which takes O(n^2log(n^2)), is there a more efficient way to do that?