Question: Given a sorted array A find all possible difference of elements from A, where each element is an integer in the range [1, ..., n]. Also, you can assume there are no duplicates. So max size of the array will be <= n.
Note: Total possible differences will be in the range of [1, ..., n-1] because of the above constraints.
Example (for N=12):
Input: 1, 6, 10, 12
Output: 2, 4, 5, 6, 9, 11
The question is similar to this one, except n is the no. of elements in that question, not the upper bound of the element.
There's also an answer in the same question, this one: https://stackoverflow.com/a/8455336/2109808
This guy claims it can really be done in O(nlogn) using fft and self convolution, but I don't get it, and it also seems to be incorrect when I try it on online convolution calculators (like this one).
So, does anyone know how this can be achieved in O(nlogn)?
Thank you in advance :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…