Algorithm to find a duplicate entry in constant space and O(n) time

后端 未结 8 2075
我在风中等你
我在风中等你 2020-12-16 04:30

Given an array of N integer such that only one integer is repeated. Find the repeated integer in O(n) time and constant space. There is no range for the value of integers or

8条回答
  •  被撕碎了的回忆
    2020-12-16 05:06

    If the range of the integers is bounded, you can perform a counting sort variant in O(n) time. The space complexity is O(k) where k is the upper bound on the integers(*), but that's a constant, so it's O(1).

    If the range of the integers is unbounded, then I don't think there's any way to do this, but I'm not an expert at complexity puzzles.

    (*) It's O(k) since there's also a constant upper bound on the number of occurrences of each integer, namely 2.

提交回复
热议问题