c++ - Solving an Algorithmic Challenge involving Differences between Pairs -


the below algorithmic problem came with, motivated harder problem not solve. unfortunately, cannot solve 1 either.

you given n numbers in increasing order (n between 1 , 3*10^5, linear running time necessary). each number range 1 3*10^5 well. each number 1 3*10^5, denote z, want find how many pairs of numbers have pairwise difference of z. read examples below more clarity.

numbers: 1,6,9
answer: 1 pair difference of 5 (1,6), 1 pair difference of 3 (6, 9), , 1 pair difference of 8 (1,9)

numbers: 1,2,3,4
answer: 3 pairs difference of 1 (1,2 2,3 3,4), 2 pairs difference of 2 (1,3 2,4), , 1 pair difference of 3 (1,4).

is there data-structure can used solve problem in linear time?

here's o(n polylog n)-time algorithm compute counts z (assuming have @ n numbers between 1 , n).

prepare 2 polynomials p(x) = sum_{d in list} x^d , q(x) = sum_{d in list} x^(n - d). use fast multiplication algorithm compute coefficients of polynomial product p(x) q(x). answer z, examine coefficient of x^(n + z), is

sum_{d in list} sum_{e in list} [d + n - e = n + z], 

where [d + n - e = n + z] = [d - e = z] 1 if d - e = z , 0 otherwise.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -