I thought about it, came up with one of the n log(n) algorithms, but now don't feel obliged to type it up. :-)

Cheers,
Ben