11

Given a sorted array of integers, can we build a sorted array of the sums of all pairs in O(n^2)?

A trivial solution would be to build the array of sums in O(n^2) and then to sort it in O(n^2 (log(n^2)) = O(n^2 logn) time.

Another solution would be to build n sorted arrays of n numbers each - in O(n^2), and merge them in O(n^2 logn) time (see here for example).

Can we do better?

Community
  • 1
  • 1
Lior Kogan
  • 18,061
  • 4
  • 49
  • 79
  • Um, why would the first solution be O(n^2 (log^2)n) time? Shouldn't it be O(n^2 log(n^2)) ( = O(n^2 log(n)) ) to sort an array of length n^2? Or do you have in mind using something faster than comparison sorting? – Ted Hopp Jul 18 '13 at 20:48
  • 9
    a.k.a. [Sorting X + Y](http://cs.smith.edu/~orourke/TOPP/P41.html) – David Eisenstat Jul 18 '13 at 20:48
  • @DavidEisenstat so the problem is over unless we can think of a solution that doesn't exist yet – aaronman Jul 18 '13 at 20:49
  • @aaronman Yes, the best result known for a comparison sort is sorting with n^2 comparisons in time n^2 log n. – David Eisenstat Jul 18 '13 at 20:51
  • 1
    @DavidEisenstat maybe this can be the first time SO contributes something useful to the world – aaronman Jul 18 '13 at 20:52
  • @TedHopp: You are correct. I'll fix it. – Lior Kogan Jul 18 '13 at 20:54
  • @DavidEisenstat: Thanks! I guess Mortianna was correct - We're doomed. – Lior Kogan Jul 18 '13 at 21:01
  • @EdHeal yeah 39 year olds get a lot of hw, sorry for ratting you out Lior – aaronman Jul 18 '13 at 21:02
  • If you don't care about duplicates, you can do it by simply inserting the result into an array at index *k*, where *k* = `i+j` in O(n^2) time, then compacting the array in O(n) time. – Chris Heald Jul 18 '13 at 21:05
  • @EdHeal Besides this obviously not being homework (it is an open problem...), the question clearly says what OP has tried. – interjay Jul 18 '13 at 21:05
  • @LiorKogan what do you mean, if you want me to delete it thats fine – aaronman Jul 18 '13 at 21:06
  • @ChrisHeald `i+j` could be a very large number, you can't create or compact an array of that size in `O(n^2)`. – interjay Jul 18 '13 at 21:07
  • @interjay Can you elaborate? Given that the original array has *n* elements, you just have to allocate an array *n^2* elements in size. You can keep a count of how many new vs override insertions you have, which lets you get the size of the compact array target, which is another simple allocation. – Chris Heald Jul 18 '13 at 21:10
  • @ChrisHeald Assuming that by `i+j` in your original comment you meant the sum of each two elements, an array of size n^2 won't be enough because `i+j` can be more than n^2. – interjay Jul 18 '13 at 21:12
  • Doh, right; well, you can still allocate an array of the size of `n[-1] + n[-1]` (where -1 means the last element of the array), no? It's not a particularly memory-efficient solution for large values of `n[-1]`, granted, but it is time-efficient assuming memory allocation is O(n). – Chris Heald Jul 18 '13 at 21:14
  • @DavidEisenstat: Sometimes a link is a good answer. I would accept it. I'm afraid that this question will remain open for a long time... – Lior Kogan Jul 18 '13 at 21:15
  • 1
    @ChrisHeald That would give you an O(n^2 + arr[-1]) solution instead of an O(n^2) solution, and arr[-1] could be 2^n for example... This wouldn't be an open problem if there was such a simple solution. – interjay Jul 18 '13 at 21:18
  • I realize that the open problem exists and am not pretending to solve it - I'm merely exploring other methods by which the algorithm runtime might be improved by further defining the requirements. :) – Chris Heald Jul 18 '13 at 21:26

1 Answers1

8

This is an open problem known in the literature as Sorting X + Y. The best result known is an O(n^2 log n)-time algorithm that uses O(n^2) comparisons, due to Lambert and Steiger--Streinu.

David Eisenstat
  • 52,844
  • 7
  • 50
  • 103
  • @aaronman Yes, it's possible that there's an O(n^2)-time algorithm, and as far as I know, the existence of such an algorithm would have no "disastrous" consequences the way P = NP would. That there exists an O(n^2)-comparison algorithm makes life difficult for would-be provers of lowerbounds. – David Eisenstat Jul 18 '13 at 21:25
  • @DavidEisenstat: On second thought, maybe X+X (my case) is a special case, where a better solution is already known, or can be achieved? – Lior Kogan Jul 19 '13 at 06:37
  • @LiorKogan There's a linear-time reduction from the general case to your special case. – David Eisenstat Jul 20 '13 at 12:04