5

I have a sorted list of integers, and I wish to insert into this list any element, in constant time. I'm allowed to do some pre-processing, as long as it will let me run this operation in constant time (i.e., no matter how many times I repeat this operation after the pre-processing, it should run in constant time).

How can this be done?

Edit : I can think of a couple of more constraints to make it a bit less ambiguous, and a bit more challenging to solve -

  1. list needs to be maintained in sorted order post-insertion(s)
  2. Or, list needs to somehow support max/min operations in constant time post insertion(s).
aspen100
  • 965
  • 11
  • 22
  • 1
    Check [this answer](http://stackoverflow.com/a/1933099/115272). – Jacob Seleznev Oct 14 '14 at 02:19
  • If the range of your integer values are small - you can use a bucket per integer value. When you insert an value, you just index into the bucket by the value (constant time operation) and add it to the bucket (also constant time). – Jason L Oct 14 '14 at 02:21
  • 1
    Jacob, that's only O(1) once you know where you want to insert. It's O(n) to _find_ that position. Jason, that's not bad but the "any element" in the question seems a little worrisome - the range may not be able to be limited. – paxdiablo Oct 14 '14 at 02:21
  • Do you need random access(access element by index)? If only max/min is required, `priority queue` may be a good choice. I know this post is old, I was just searching how huffman coding could be implemented in O(nlogn). [Priority_queue-wiki](https://en.wikipedia.org/wiki/Priority_queue#Summary_of_running_times) / [huffman](https://www.includehelp.com/algorithms/huffman-coding-algorithm-example-and-time-complexity.aspx) – Kirk Jun 20 '19 at 07:17

2 Answers2

6

You can put the integers into a radix tree, treating the integers as bit strings. With a radix of 2 and a list of 32-bit integers you'll have a maximum tree depth of 32, which is your constant factor for constant-time insertion (you wouldn't ordinarily do something like this because the constant factor for the radix tree is probably going to be larger than the log factor of a balanced binary tree, plus all of the bit twiddling you'll need to do in for the radix tree is going to be costly)

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
2

Use a LinkedHashMap. Decide how many elements are stored per hashcode (lets say integers 0-10 are hash(1), integers 11-20 are hash(2) and so on...)

When you read an integer, calculate its hash O(1) -> access the hashmap (roughly O(1)) -> insert to list as first element O(1). If you want to allow duplicates extend each element in the list into its own list or use a counter for each element.

Muli Yulzary
  • 2,559
  • 3
  • 21
  • 39
  • a good enough solution for most cases, but what if the range of the integers isn't known? is it still constant time? – aspen100 Oct 14 '14 at 05:08
  • Yes because whatever integer it is, you insert it at the head of the linked list so its O(1) – Muli Yulzary Oct 14 '14 at 13:18
  • 1
    No, because then you disturb the sorted order of the list. The problem is more challenging to do in O(1) time when you want to maintain the list, and support other operations like max/min in constant time as well - but your solutions is adequate as long as the bucket size isn't within orders of magnitude of the size of the problem. – aspen100 Oct 14 '14 at 18:52
  • You didn't specify those limitations at first – Muli Yulzary Oct 14 '14 at 23:17
  • 2
    You're right, but without maintaining order, the solution is extremely trivial - there is no need even for a hashmap in that case. I can simply use a linkedlist/deque and keep adding elements in constant time. – aspen100 Oct 16 '14 at 18:41