Finding repeating signed integers with O(n) in time and O(1) in space

后端 未结 7 480
慢半拍i
慢半拍i 2021-02-04 08:49

(This is a generalization of: Finding duplicates in O(n) time and O(1) space)

Problem: Write a C++ or C function with time and space complexities of O(n) and O(1) respec

7条回答
  •  一向
    一向 (楼主)
    2021-02-04 09:29

    The O(1) space constraint is intractable.

    The very fact of printing the array itself requires O(N) storage, by definition.

    Now, feeling generous, I'll give you that you can have O(1) storage for a buffer within your program and consider that the space taken outside the program is of no concern to you, and thus that the output is not an issue...

    Still, the O(1) space constraint feels intractable, because of the immutability constraint on the input array. It might not be, but it feels so.

    And your solution overflows, because you try to memorize an O(N) information in a finite datatype.

提交回复
热议问题