# k-Maximum Subarrays for Small k: Divide-and-Conquer Made Simpler

## Abstract

Given an array *A* of *n* real numbers, the maximum subarray problem is to find a contiguous subarray which has the largest sum. The *k*-maximum subarrays problem is to find *k* such subarrays with the largest sums. For the 1−maximum subarray the well known divide-and-conquer algorithm, presented in most textbooks, although suboptimal, is easy to implement and can be made optimal with a simple change that speeds up the combine phase. On the other hand, the only known divide-and-conquer algorithm for \(k > 1\), that is efficient for small values of *k*, is difficult to implement, due to the intricacies of the combine phase. In this paper, we show how to simplify the combine phase considerably while preserving the overall running time.

In the process of designing the combine phase of the algorithm we provide a simple, sublinear, \(O(\sqrt{k} \log ^{3} k)\) time algorithm, for finding the *k* largest sums of \(X + Y\), where *X* and *Y* are sorted arrays of size *n* and \(k \le n^2\). The *k* largest sums are implicitly represented and can be enumerated with an additional *O*(*k*) time.

Our solution relies on simple operations such as merging sorted arrays, binary search and selecting the \(k^{th}\) smallest number in an array. We have implemented our algorithm and report excellent performance as compared to previous results.

## Keywords

k-Maximum subarrays Divide and conquer X + Y Sublinear## References

- 1.Agrawal, R., Imieliński, T., Swami, A.: Mining association rules between sets of items in large databases. In: Acm SIGMOD Record, vol. 22, pp. 207–216. ACM (1993)Google Scholar
- 2.Bae, S.E., Takaoka, T.: Improved algorithms for the k-maximum subarray problem. Comput. J.
**49**(3), 358–374 (2006)CrossRefGoogle Scholar - 3.Bengtsson, F., Chen, J.: Efficient algorithms for k maximum sums. Algorithmica
**46**(1), 27–41 (2006)MathSciNetCrossRefGoogle Scholar - 4.Bengtsson, F., Chen, J.: Ranking k maximum sums. Theor. Comput. Sci.
**377**(1–3), 229–237 (2007)MathSciNetCrossRefGoogle Scholar - 5.Bentley, J.: Algorithm design techniques. Commun. ACM
**27**(9), 865–871 (1984)MathSciNetCrossRefGoogle Scholar - 6.Bentley, J.: Programming pearls: algorithm design techniques. Commun. ACM
**27**(9), 865–873 (1984)MathSciNetCrossRefGoogle Scholar - 7.Blum, M., Floyd, R.W., Pratt, V.R., Rivest, R.L., Tarjan, R.E.: Time bounds for selection. J. Comput. Syst. Sci.
**7**(4), 448–461 (1973)MathSciNetCrossRefGoogle Scholar - 8.Chazelle, B.: The soft heap: an approximate priority queue with optimal error rate. J. ACM (JACM)
**47**(6), 1012–1027 (2000)MathSciNetCrossRefGoogle Scholar - 9.Cheng, C.H., Chen, K.Y., Tien, W.C., Chao, K.M.: Improved algorithms for the k maximum-sums problems. Theor. Comput. Sci.
**362**(1–3), 162–170 (2006)MathSciNetCrossRefGoogle Scholar - 10.Cormen, T.H.: Introduction to Algorithms. MIT press, Cambridge (2009)zbMATHGoogle Scholar
- 11.Frederickson, G.N., Johnson, D.B.: The complexity of selection and ranking in x+ y and matrices with sorted columns. J. Comput. Syst. Sci.
**24**(2), 197–208 (1982)MathSciNetCrossRefGoogle Scholar - 12.Frederickson, G.N., Johnson, D.B.: Generalized selection and ranking: sorted matrices. SIAM J. Comput.
**13**(1), 14–30 (1984)MathSciNetCrossRefGoogle Scholar - 13.Fukuda, T., Morimoto, Y., Morishita, S., Tokuyama, T.: Data mining using two-dimensional optimized association rules: scheme, algorithms, and visualization. ACM SIGMOD Record
**25**(2), 13–23 (1996) CrossRefGoogle Scholar - 14.Grenander, U.: Pattern analysis: lectures in pattern theory 2. Appl. Math. Sci.
**24**(1978)Google Scholar - 15.Kaplan, H., Kozma, L., Zamir, O., Zwick, U.: Selection from heaps, row-sorted matrices and \( x+ y \) using soft heaps. arXiv preprint arXiv:1802.07041 (2018)