Please post it back if there are quick method. Actually, last time one friend got google interview, and was asked this question. Some say it is nlogm, if n << m. Also see here: http://groups.google.com/group/algogeeks/browse_frm/thread/6314d3437b1a172b?hl=en