alphaFeedback

WorksComputer Science

On the Largest Cardinality Subset Sum

AI contributions
Not recorded for this paper
View details

Abstract

We study Largest Cardinality Subset Sum (LCSS): given nn nonnegative integers and a target tt, compute a subset summing to tt with maximum cardinality. We also consider its all-targets version (ATLCSS), which asks for this maximum for every sum s∈{1,…,t}s\in\{1,\ldots,t\}. The LCSS problem sits squarely between the subset sum and the knapsack problem. A straightforward dynamic-programming algorithm runs in time O(nt)O(nt) and solves ATLCSS, and using Jin and Bringmann (both in STOC 2024, independent work) 00-11 knapsack results we achieve a O~(n+amax⁡2)\widetilde{O}(n + a_{\max}^2)-time deterministic algorithm for LCSS. Our main result is a randomized O~(n+n t)\widetilde{O}(n+\sqrt{n}\,t)-time algorithm for LCSS. Then, we complement this result with a randomized O~(n+t3/2)\widetilde{O}(n+t^{3/2})-time algorithm for ATLCSS. Together, our two algorithms give a O~(n+tmin⁡{n,t})\widetilde{O}(n+t\min\{\sqrt{n},\sqrt{t}\})-time algorithm for LCSS. As an application of ATLCSS, we solve the search version of Projection Subset Sum, introduced by Dutta and Rajasree (SYNSAC 2023), in O~(n+t3/2)\widetilde{O}(n+t^{3/2}) time for every fixed ℓp\ell_p norm with p≥1p\geq 1. For ℓ1\ell_1, we obtain O~(n+t)\widetilde{O}(n+t) time, removing the unique-representation assumption required by their algorithm. Both algorithms output an optimal modified vector and a witnessing subset.

2012 ACM Subject Classification Theory of computation →\to Design and analysis of algorithms

Funding Anubhav Dhar: Supported by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) grant number 559177164.

Pranjal Dutta: Supported by Nanyang Assistant Professorship as well as by the SUG Grant (#025774-00001), funded by Nanyang Technological University Singapore.

Mahesh Sreekumar Rajasree: Funded by the European Union (ERC, LACONIC, 101041207). Views and opinions expressed are those of the authors only and do not necessarily reflect those of the European Union or the European Research Council. Neither the European Union nor the granting authority can be held responsible for them.

Karol Węgrzycki: Supported by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) grant number 559177164.

Introduction

The Subset Sum problem asks whether, given nonnegative integers a1,…,ana_1,\ldots,a_n and a target tt, there exists a subset of the input items that sums exactly to tt. It is a basic example of an NP-complete problem that admits a pseudopolynomial algorithm: Bellman’s dynamic program takes O(nt)O(nt) time [3]. Subsequent work has substantially improved the dependence on nn and tt. Koiliaris and Xu gave a deterministic O~(nt)\widetilde{O}(\sqrt{nt})-time algorithm [15], and Bringmann [4] and, later, Jin and Wu [14] gave randomized O~(n+t)\widetilde{O}(n+t)-time algorithms. Recently, Chan derandomized these results and gave a deterministic O~(t)\widetilde{O}(t)-time algorithm for Subset sum [6]. These algorithms determine which sums in {0,…,t}\{0,\ldots,t\} are attainable, i.e. appear as a sum of some subset of input items. A natural next question is what can be learned about the subsets attaining a given target. We study the maximum size of such a subset. Items are distinguished by their indices, even when their values are equal.

Definition 1 (Largest Cardinality Subset Sum (LCSS)). Given nonnegative integers a1,…,ana_1,\ldots,a_n and a positive target tt, LCSS asks for max⁡{∣S∣:S⊆{1,…,n},∑i∈Sai=t}\max\{|S|:S\subseteq\{1,\ldots,n\},\sum_{i\in S}a_i=t\}, else report if tt is unattainable.

Thus LCSS refines the usual decision problem: it distinguishes two attainable representations of the same target by their cardinalities. It also has a direct interpretation as a structured 0-1 knapsack problem: we give item ii weight aia_i and profit Mai+1Ma_i+1, where M>nM>n, and set the knapsack capacity to tt. Every subset of weight exactly tt has profit Mt+∣S∣Mt+|S|, whereas every subset of weight at most t−1t-1 has profit at most M(t−1)+n<MtM(t-1)+n<Mt. Consequently, if tt is attainable, an optimal knapsack solution is a subset that sums to tt and has maximum cardinality among all the subsets that sum to tt; if it is unattainable, the optimal profit is below MtMt. This embedding explains why recording only attainable sums, as in ordinary subset sum, does not immediately give the desired optimum. In fact, at the fine-grained level, this places LCSS in between the subset sum and the knapsack problem; LCSS is at least as hard as subset sum, and knapsack is at least as hard as LCSS. These observations make LCSS an interesting problem to study.

0-1 knapsack for single capacity: Jin [12] gave a deterministic algorithm that runs in time O~(n+amax⁡2)\widetilde{O}(n+a_{\max}^2), whereas Bringman [5] also (independently) gave a deterministic O~(n+amax⁡2)\widetilde{O}(n+a_{\max}^2)-time algorithm for the bounded knapsack problem (where each item comes with a multiplicity). Here amax⁡a_{\max} denotes the maximum weight item. Using the above reduction and these two algorithms, we get O~(n+amax⁡2)\widetilde{O}(n+a_{\max}^2)-time algorithm for LCSS.

There is also a useful analogy with change-making. The change-making problem minimizes the number of coins summing to a target, with unlimited use of each denomination. In LCSS, each input item can be used at most once, and the number of selected items is maximized. Both problems invite a distinction between a single specified target and all targets up to a bound. This distinction has proved consequential for change-making: Chan and He obtained near-linear-in-tt algorithms for the single-target problem and an O~(t4/3)\widetilde{O}(t^{4/3})-time algorithm for its all-targets version [7]. We likewise study both versions of LCSS.

Definition 2 (All-Targets Largest Cardinality Subset Sum (AT-LCSS)). Given nonnegative integers a1,…,ana_1,\ldots,a_n and a positive integer tt, AT-LCSS asks, for every s∈{1,…,t}s\in\{1,\ldots,t\}, for the maximum cardinality of a subset S⊆{1,…,n}S\subseteq\{1,\ldots,n\} with ∑(S)=s\sum(S)=s, else report if ss is unattainable.

A straightforward dynamic program computes the optimum for every sum up to tt in O(nt)O(nt) time: when item aia_i is processed, the best cardinality at sum ss is compared with one plus the previous best cardinality at s−ais-a_i. Koiliaris and Xu [15] also discuss the problem of subset sums with cardinality information. They further provide an O~(nt)\widetilde{O}(nt)-time algorithm that gives all the attainable subset sums with complete cardinality information, not just the maximum. However, their improvement of the complexity from O~(nt)\widetilde{O}(nt) to O~(nt)\widetilde{O}(\sqrt{nt}) loses information regarding cardinalities. This suggests that solving LCSS in O~(nt)\widetilde{O}(\sqrt{nt}) time is an interesting problem. On the other hand, Axiotis and Tzamos [2] give an O(Dt)O(Dt)-time algorithm for the knapsack problem, with all weights being less than or equal to tt, where DD denotes the number of distinct items in the instance. This algorithm, with the aforementioned reduction of LCSS to knapsack, gives a O(Dt)O(Dt)-time algorithm for AT-LCSS, and consequently for LCSS too.

The subset-sum algorithms in [4, 14, 6] do not, on their own, preserve the largest cardinality for each sum. Related work has examined other forms of cardinality information: Dutta and Rajasree [9] study the Hamming weights of all solutions when the number of solutions is bounded, while Antonopoulos, Pagourtzis, Petsalakis, and Vasilakis [1] discuss algorithms for subset-sum variants with target-cardinality constraints.

Projection Subset Sum and related work

AT-LCSS has a direct application to Projection Subset Sum, introduced by Dutta and Rajasree [10]. Given a nonnegative integer vector Z=(a1,…,an)Z=(a_1,\ldots,a_n) and a nonnegative integer target tt, the search problem asks for an integer vector X=(x1,…,xn)X=(x_1,\ldots,x_n) and a subset S⊆[n]S\subseteq[n] satisfying ∑i∈Sxi=t\sum_{i\in S}x_i=t that minimize ∥Z−X∥p\lVert Z-X\rVert_p. For t>0t>0, the set SS is necessarily nonempty. Their near-linear randomized algorithm for the ℓ1\ell_1 case assumes that every attainable sum has a unique representation; they also give an O(nt)O(nt) algorithm under that assumption [10].

For a fixed nonempty subset SS, the amount by which its original sum misses tt is d=t−Σ(S)d=t-\Sigma(S). In the ℓ∞\ell_\infty case, if integer-valued coordinates may be changed without sign restrictions, the smallest possible maximum coordinate change is ⌈∣d∣/∣S∣⌉\lceil|d|/|S|\rceil. More selected items allow the discrepancy to be spread over more coordinates. For general p≥1p\ge1, the corresponding exact integer cost further depends on Σ(S)\Sigma(S) and ∣S∣|S|; see § 6. Among nonempty subsets with the same sum, a largest subset minimizes the projection cost. Therefore, an AT-LCSS solution supplies exactly the cardinality information needed to compare the best projection costs for different attainable sums, including sums above tt.

A related motivation for Projection Subset Sum comes from neural-network pruning. The strong lottery ticket hypothesis asks whether a randomly initialized network contains a subnetwork that approximates a given target network [20, 18]. Pensia et al. [19] study this question using random subset sums, while Xiong, Liao, and Kyrillidis [22] additionally allow bounded perturbations of the weights. This combines selecting existing weights with slightly modifying them to improve approximation. Projection Subset Sum captures a related question for a list of numbers: how little must the numbers be changed so that some subset sums exactly to a prescribed target?

Our results

The previous discussion already gives us O~(n+dmax⁡2)\widetilde{O}(n+d_{\max}^2)-time and O(n+Dt)O(n+Dt)-time algorithms for LCSS. Our main result is a faster pseudopolynomial-time algorithm for the regime when amax⁡=Θ~(n1/4t)a_{\max}=\widetilde{\Theta}(n^{1/4}\sqrt{t}) and D=O~(n)D=\widetilde{O}(\sqrt{n}). Note that O~(⋅)\widetilde{O}(\cdot) hides polylogarithmic factors.

Theorem 3. For any fixed integer constant c≥10c\ge10, there exists a randomized algorithm that solves LCSS on nn nonnegative integers with target tt in time O~(n+nt)\widetilde{O}(n+\sqrt{nt}), with error probability at most n−cn^{-c}.

Thus, up to polylogarithmic factors, our algorithm reduces the coefficient of tt from nn to n\sqrt{n} in the running time complexity. It computes the exact maximum cardinality of a subset summing to tt, or reports that tt is unattainable, with high probability.

Very recently, Jin and Mao [13, Proposition D.3] give a conditional result for LCSS on positive inputs. If there is an algorithm running in T(n,Δ)T(n,\Delta) that decomposes all subset sums of nn positive integers bounded by Δ\Delta into an arithmetic progression with a common [difference], then they managed to show that LCSS can be solved in O~(n+n⋅t+T(n,3tn))\widetilde{O}(n+\sqrt{n}\cdot t+T(n,3t\sqrt{n})) time. Their result would match the running time of our result, if T(n,Δ)≤O~(n+Δ)T(n,\Delta)\le\widetilde{O}(n+\Delta), however the existance of such a fast algorithm is open [13]. Theorem 3 avoids this unresolved assumption and achieves the same running-time bound unconditionally.

Our second result focuses on the all-targets version, in which the maximum cardinality must be computed for every target s∈{1,…,t}s\in\{1,\ldots,t\}.

Theorem 4. For any fixed integer constant c≥10c\ge10, there exists a randomized algorithm that solves AT-LCSS on nn nonnegative integers with target tt in time O~(n+t3/2)\widetilde{O}(n+t^{3/2}), with failure probability at most n−cn^{-c}.

This algorithm improves the discussed O(n+Dt)O(n+Dt) algorithm when D=ω~(t1/2)D=\widetilde{\omega}(t^{1/2}). The probability guarantee holds simultaneously for all targets. Beyond the time needed to read the input, the running time depends only on tt, rather than on the number of items. The two bounds are complementary. Since the all-targets algorithm also solves any single-target instance, together they yield an O~(n+tmin⁡{n,t})\widetilde{O}(n+t\min\{\sqrt{n},\sqrt{t}\})-time algorithm for LCSS.

By exploiting the cardinality information provided by AT-LCSS, we also give a randomized O~(n+t3/2)\widetilde{O}(n+t^{3/2})-time algorithm for Projection Subset Sum in the ℓp\ell_p norm (PSSp_p) for all fixed p≥1p\ge1.

Theorem 5. For fixed p≥1p\ge1, given nonnegative integers Z=(a1,…,an)Z=(a_1,\ldots,a_n) and an integer t≥0t\ge0, there is a randomized algorithm that outputs an optimal solution (X,S)(X,S) to PSSp_p in time O~(n+t3/2)\widetilde{O}(n+t^{3/2}), with failure probability at most 1/31/3.

And, for the ℓ1\ell_1 norm, we give a randomized O~(n+t)\widetilde{O}(n+t)-time algorithm using Bringmann’s subset-sum algorithm; see Theorem 27. Both the PSS algorithms output an optimal vector XX and a set SS witnessing its feasibility, with failure probability at most 1/31/3, without the unique-representation assumption.

Organization of the paper. § 2 gives an overview of our algorithms. § 3 introduces the notation and tools used. § 4 presents the algorithm for LCSS via solving TCSS. The algorithm for AT-LCSS can be found in § 5. The algorithms for Projection Subset Sum can be found in § 6. Appendix § 7 contains deferred lemmas and proofs.

Declaration on the Use of AI Tools. Generative AI tools, including ChatGPT and Claude, were used solely for proofreading, language editing, improving presentation, and identifying potential inconsistencies. All technical contributions are original to the authors, who independently verified relevant AI-generated suggestions and take full responsibility for the manuscript.

Technical Overview

Previously, Bringmann [4] gave an O~(n+t)\widetilde{O}(n+t)-time algorithm to output all possible subset sums capped by an integer tt, in an application of nn elements. This was later simplified by Jin and Wu [14], and then derandomized by Chan [6]. For this paper, we give a bivariate equivalent result of [14], which computes both cardinalities and sums of all possible subsets with caps in both cardinality and the sum.

Lemma 6. Given nonnegative integers a1,…,ana_1,\ldots,a_n, positive integers kk and tt, and a parameter δ∈(0,1)\delta\in(0,1), there is a randomized algorithm with one-sided error probability at most δ\delta that computes the set {(Σ(S),∣S∣):S⊆{1,…,n},∣S∣≤k,Σ(S)≤t}\{(\Sigma(S), |S|) : S \subseteq\{1,\ldots,n\}, |S| \le k, \Sigma(S) \le t\} in time

O((n+kt)polylog⁡(n+kt+1/δ)).O((n+kt)\operatorname{polylog}(n+kt+1/\delta)).

The returned set is always a subset of {(Σ(S),∣S∣):S⊆{1,…,n},∣S∣≤k,Σ(S)≤t}\{(\Sigma(S), |S|) : S \subseteq\{1,\ldots,n\}, |S| \le k, \Sigma(S) \le t\}.

A proof idea is as follows: Consider Bringmann’s algorithm [4]. Essentially, the algorithm consists of a collection of convolutions to keep track of sums. Whenever a sum exceeds tt, this sum is dropped from further consideration. One can modify Bringmann’s algorithm to simply consider pairs of cardinalities and sums, then convolve these sets of pairs, and drop pairs which exceed either the cardinality bound or the sum cap. The resulting algorithm has the required running time. Similarly, Jin and Wu’s algorithm [14] can also be modified accordingly to prove Lemma 6; for completeness, we provide a complete proof in Appendix A.2.

Solving Largest Cardinality Subset Sum

We start by focusing on the intermediate problem of finding whether a subset of a given sum and a given cardinality exists.

References

  1. [1]Antonis Antonopoulos, Aris Pagourtzis, Stavros Petsalakis, and Manolis Vasilakis. Faster algorithms for k-subset sum and variations. Journal of Combinatorial Optimization, 45(1):24, 2023.arxiv.org/abs/2112.04244
  2. [2]Kyriakos Axiotis and Christos Tzamos. Capacitated dynamic programming: Faster knapsack and graph algorithms. arXiv preprint arXiv:1802.06440, 2018.arxiv.org/abs/1802.06440
  3. [3]Richard Bellman. Dynamic Programming. Princeton University Press, 1957.pubmed.ncbi.nlm.nih.gov/17730601
  4. [4]Karl Bringmann. A near-linear pseudopolynomial time algorithm for subset sum. In Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms, pages 1073–1084. SIAM, 2017.arxiv.org/abs/1610.04712
  5. [5]Karl Bringmann. Knapsack with small items in near-quadratic time. In Proceedings of the 56th Annual ACM Symposium on Theory of Computing, pages 259–270, 2024.arxiv.org/abs/2308.03075
  6. [6]Timothy M. Chan. Derandomizing pseudopolynomial algorithms for subset sum. In Proceedings of the 2026 Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 3600–3610. SIAM, 2026.
  7. [7]Timothy M. Chan and Qizheng He. More on change-making and related problems. In 28th Annual European Symposium on Algorithms (ESA 2020), volume 173 of Leibniz International Proceedings in Informatics (LIPIcs), pages 29:1–29:14, 2020. doi:10.4230/LIPIcs.ESA.2020.29.DOI
  8. [8]Thomas H Cormen, Charles E Leiserson, Ronald Rivest, and Clifford Stein. Algorithmen: eine Einführung. Walter de Gruyter GmbH & Co KG, 2025.
  9. [9]Pranjal Dutta and Mahesh Sreekumar Rajasree. Efficient reductions and algorithms for variants of subset sum. arXiv preprint arXiv:2112.11020, 2022.arxiv.org/abs/2112.11020
  10. [10]Pranjal Dutta and Mahesh Sreekumar Rajasree. On the variants of subset sum: Projected and unbounded. In 2023 25th International Symposium on Symbolic and Numeric Algorithms for Scientific Computing (SYNASC), pages 86–93. IEEE, 2023.DOI
  11. [11]Michael R. Garey and David S. Johnson. Computers and Intractability: A Guide to the Theory of NP-Completeness. W. H. Freeman, 1979.
  12. [12]Ce Jin. 0-1 knapsack in nearly quadratic time. In Proceedings of the 56th Annual ACM Symposium on Theory of Computing, pages 271–282, 2024.
  13. [13]Ce Jin and Xiao Mao. Subset Sum in Almost-Linear Time. arXiv preprint arXiv:2610.20742v1, 2026. URL: https://arxiv.org/abs/2610.20742v1.
  14. [14]Ce Jin and Hongxun Wu. A simple near-linear pseudopolynomial time randomized algorithm for subset sum. In 2nd Symposium on Simplicity in Algorithms (SOSA 2019), pages 17:1–17:6. Schloss Dagstuhl–Leibniz-Zentrum für Informatik, 2019. doi:10.4230/OASIcs.SOSA.2019.17.DOI
  15. [15]Konstantinos Koiliaris and Chao Xu. Faster pseudopolynomial time algorithms for subset sum. ACM Transactions on Algorithms, 15(3):40:1–40:20, 2019. doi:10.1145/3329863.DOI
  16. [16]Eugene L Lawler. Fast approximation algorithms for knapsack problems. Mathematics of operations research, 4(4):339–356, 1979.
  17. [17]Andrea Lincoln, Adam Polak, and Virginia Vassilevska Williams. Monochromatic triangles, intermediate matrix products, and convolutions. In ITCS, volume 151 of LIPIcs, pages 53:1–53:18. Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 2020.DOI
  18. [18]Eran Malach, Gilad Yehudai, Shai Shalev-Schwartz, and Ohad Shamir. Proving the lottery ticket hypothesis: Pruning is all you need. In International conference on machine learning, pages 6682–6691. PMLR, 2020.
  19. [19]Ankit Pensia, Shashank Rajput, Alliot Nagle, Harit Vishwakarma, and Dimitris Papailiopoulos. Optimal lottery tickets via subset sum: Logarithmic over-parameterization is sufficient. In Advances in Neural Information Processing Systems, volume 33, pages 2599–2610, 2020.DOI
  20. [20]Vivek Ramanujan, Mitchell Wortsman, Aniruddha Kembhavi, Ali Farhadi, and Mohammad Rastegari. What’s hidden in a randomly weighted neural network? In 2020 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), pages 11890–11899. IEEE, 2020.arxiv.org/abs/1911.13299
  21. [21]Sebastien Roch. Modern discrete probability: An essential toolkit. Cambridge University Press, 2024.DOI
  22. [22]Zheyang Xiong, Fangshuo Liao, and Anastasios Kyrillidis. Strong lottery ticket hypothesis with ε–perturbation. In Proceedings of The 26th International Conference on Artificial Intelligence and Statistics, volume 206, pages 6879–6902. PMLR, 2023.arxiv.org/abs/2210.16589

Paper details

vlnc:W_p5qwcp2k
v1 · latestHistory (1)

published · Fresh — added to the Hub within the last 30 days

added ·

Usage

views · 10

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

CC BY 4.0

Original source

  • Computer Science

Not recorded for this paper · View statement →

Looking for related papers…
Contents