【发布时间】:2012-06-13 14:21:30
【问题描述】:
我知道类似的问题可能会被问很多次,但又有所不同。
给定一个未排序的整数数组,其值为1 to N-1,其中有一个重复元素(N 是数组的大小,最大值为N-1,因为存在重复元素)。找到重复元素的最佳方法是什么?如果可能的话O(log n).`
我知道O(n) 的解决方案,但是有什么办法可以在O(log n) 时间内解决这个问题吗?
O(n) 解:
1) Find sum of first N-1 natural numbers using N(N-1)/2, lets say it SUM1
2) Find sum of all the elements of the arrays, let say it SUM2
Duplicate = SUM2 - SUM1
【问题讨论】: