Problem: input is a (not necessarily sorted) sequence S = k1, k2, ..., kn of n arbitrary numbers. Consider the collection C of n² numbers of the form min{ki,kj}, for 1 <=
Yes, good puzzle. We can find median developing on the lines you said.
In C we have 1 occurence of max(k), 3 occurrence of next highest, 5 of next highest and so on
If we ordered elements of C, number of elements on the left of mth highest number is m^2 (sum of odd numbers)
The numbers that we are interested in (to calculate median) a. If n is odd is (n^2+1)/2 = alpha b. If n is even then alpha1 = n^2/2 and alpha2 = n^2/2+1 but alpha1=n^2/2 is never a square number => the number immediately on the right of alpha1 is equal to alpha1 (sum of first m odd numbers is square) => alpha1=alpha2.
So it boils down to determining m such that m^2 (sum of first m odd numbers) is just higher than (n^2/2)
So it boils down to determining m=ceiling(n/sqrt(2) and mth highest number in original sequence. (Whether to find mth highest or (n-m-1)th lowest is optimization).
We can easily find mth highest number (just keep noting first m largest number from left) or use median of medians algortithm to do it in linear time.