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

后端 未结 7 476
慢半拍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:19

    Since you have an array of integers you can use the straightforward solution with sorting the array (you didn't say it can't be modified) and printing duplicates. Integer arrays can be sorted with O(n) and O(1) time and space complexities using Radix sort. Although, in general it might require O(n) space, the in-place binary MSD radix sort can be trivially implemented using O(1) space (look here for more details).

提交回复
热议问题