Write a program to find 100 largest numbers out of an array of 1 billion numbers

前端 未结 30 2204
深忆病人
深忆病人 2020-11-29 14:04

I recently attended an interview where I was asked \"write a program to find 100 largest numbers out of an array of 1 billion numbers.\"

I was only able to give a br

30条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 14:49

    Another O(n) algorithm -

    The algorithm finds the largest 100 by elimination

    consider all the million numbers in their binary representation. Start from the most significant bit. Finding if the MSB is 1 can be a done by a boolean operation multiplication with an appropriate number. If there are more than 100 1's in these million eliminate the other numbers with zeros. Now of the remaining numbers proceed with the next most significant bit. keep a count of the number of remaining numbers after elimination and proceed as long as this number is greater than 100.

    The major boolean operation can be an parallely done on GPUs

提交回复
热议问题