Given numbers from 1 to 2^32-1, one is missing. How to find the missing number optimally?

前端 未结 5 1054
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 03:58

You are given 2^32-2 unique numbers that range from 1 to 2^32-1. It\'s impossible to fit all the numbers into memory (thus sorting is not an option). You are asked to find t

5条回答
  •  感情败类
    2020-12-14 04:39

    Add all the numbers you are given up using your favourite big integer library, and subtract that total from the sum of all the numbers from 1 to 2^32-1 as obtained from the sum of arithmetic progression formula

提交回复
热议问题