# Maxima of diagonals in a column wise and row wise sorted matrix

Let $\{a_i\}$ and $\{b_i\}$ be non-decreasing sequences of non-negative integers.

How fast can one find $$c_j=\max_{0 \leq i< j}\{a_i+b_{j-i-1}\}$$ for all $0\leq j\leq n-1$?

Naively, it takes $O(n^2)$ time, but I'm hoping monotonicity can help here.

It's easy to observe $\{c_i\}$ is also non-decreasing. If we consider the matrix $M$ where $M_{i,j} = a_i+b_j$, then it is a matrix sorted in both row and column direction, and we are searching for the maximum element in every diagonal.

However, if it's an arbitrary column wise and row wise sorted matrix, this problem require $\Omega(n^2)$ time.

Proof: Let all the numbers below the main diagonal be $\infty$. The elements in the $k$th diagonal are randomly numbers from $(k,k+1)$. Reading any entry provides no information for any other entry.

Edit: This problem is much harder than I anticipated. We can model this problem as a convolution problem over the semiring $(\min,+)$ (take the dual, search for min instead of max), and it can be solved in $O(\frac{n^2}{\log n})$ time according to a Ryan Williams's answer on mathoverflow. It doesn't use the information that the sequence is non-decreasing though.

Great question! "Distance Transforms of Sampled Functions", by Felzenszwalb and Huttenlocher shows how to compute this in $O(n \lg n)$ time.
"Necklaces, Convolutions, and X+Y", By Bremner et al. shows a $O\left(\frac{n^2}{\frac{\lg^2 n}{(\lg \lg n)^3}}\right)$ algorithm for this problem on the real RAM and a $O(n \sqrt{n})$ algorithm in the nonuniform linear decision tree model.
• I see that my problem is a special minimum convolution problem. However the paper doesn't say much about the general case. It requires one of the function to be convex/concave in order to compute in $O(n \log n)$ time. – Chao Xu Nov 22 '13 at 12:11
• I think the first one is $O(\frac{n^2}{\log n})$, the bound you give is for the $(median,+)$ version. – Chao Xu Nov 22 '13 at 20:32