[Tutorial] CSES — Same Sum Subsets

Revision en1, by abner_vidal, 2026-04-07 02:55:01

Introduction

In this problem, we are asked to find two disjoint subsets of an array such that both subsets have the same sum.

The key constraints are: - $$$3 \le n \le 40$$$ - $$$\sum_{i=1}^{n} x_i \le 2^{n} - 2$$$

After solving it on my own, I looked through several other submissions but couldn’t find an approach similar to mine. I found my solution quite elegant, so I decided to share it here.


Prerequisites

  • Pigeonhole Principle
  • Fast Fourier Transform (FFT)
  • Meet in the middle

Observation 1

It is sufficient to find two distinct subsets $$$A$$$ and $$$B$$$ such that

$$$ \sum_{x \in A} x = \sum_{x \in B} x. $$$

Indeed, let

$$$ A' = A \setminus B \quad \text{and} \quad B' = B \setminus A. $$$

Then $$$A'$$$ and $$$B'$$$ are disjoint, and

$$$ \sum_{x \in A'} x = \sum_{x \in B'} x. $$$

Therefore, any two distinct subsets with equal sum can be transformed into two disjoint subsets with equal sum.

Observation 2

The total number of non empty subsets is $$$2^{n}-1$$$.

On the other hand, since

$$$ \sum_{i=1}^{n} x_i \le 2^{n}-2, $$$

the number of possible subset sums is at most $$$2^{n}-2$$$.

Hence, there are more subsets than possible sums, so by the Pigeonhole Principle, there exist two distinct subsets with the same sum. By observation 1, this implies that a valid solution always exists.

Finding the Collision

The remaining task is to efficiently find two subsets with equal sum.

To achieve this, we use a meet in the middle approach. We split the array into two halves, say $$$L$$$ and $$$R$$$, each of size at most $$$20$$$.

For each half, we construct a polynomial where the coefficient of $$$x^s$$$ represents the number of subsets with sum $$$s$$$. Formally, for a set $$$S$$$, define

$$$ P_S(x) = \sum_{T \subseteq S} x^{\sum_{a \in T} a}. $$$

We compute $$$P_L(x)$$$ and $$$P_R(x)$$$, and then multiply them:

$$$ P(x) = P_L(x) \cdot P_R(x). $$$

The coefficient of $$$x^k$$$ in $$$P(x)$$$ represents the number of subsets of the full array whose sum is $$$k$$$. Using Fast Fourier Transform (FFT), this multiplication can be done efficiently.

A collision (i.e., a sum $$$k$$$ with coefficient at least $$$2$$$) guarantees the existence of two distinct subsets with the same sum, which, by Theorem 1, yields two disjoint subsets with equal sum.

Polynomial Compression

A direct construction of the polynomial is still too large, since the maximum possible sum can be as large as $$$10^{12}$$$, making the degree of the polynomial infeasible.

To address this, we compress the polynomial by working modulo $$$x^k$$$ for a suitably chosen integer $$$k$$$. That is, instead of tracking exact sums, we only track them modulo $$$k$$$:

$$$ P_S(x) \bmod (x^k). $$$

This effectively reduces the degree of the polynomial to at most $$$k$$$, allowing us to perform all computations efficiently using FFT.

Although this introduces collisions between different sums that are congruent modulo $$$k$$$, this is acceptable: we are only interested in finding any collision. Once a collision is detected modulo $$$k$$$, we can recover the actual subsets and verify that their sums are equal.

By choosing $$$k$$$ appropriately, we ensure a high probability (or guarantee, depending on bounds) of detecting a valid collision while keeping the computation tractable.

Recovering the Answer

Finally, we explain how to recover an explicit solution from the compressed polynomial.

After computing the polynomial modulo $$$x^k - 1$$$, each coefficient represents the number of subsets whose sum is congruent to a given value modulo $$$k$$$. That is, for each residue $$$r$$$, we know how many subsets satisfy

$$$ \sum_{x \in S} x \equiv r \pmod{k}. $$$

By the Pigeonhole Principle, since the total number of subsets is large compared to $$$k$$$, there must exist a residue $$$r$$$ whose coefficient exceeds the expected average. In particular, there exists some $$$r$$$ such that multiple distinct subsets map to it.

We then focus on this residue class and explicitly enumerate subsets (typically via meet-in-the-middle) whose sums are congruent to $$$r \pmod{k}$$$, until we find two distinct subsets with the same exact sum.

Once such a pair is found, we apply Theorem 1 to transform them into two disjoint subsets with equal sum.

Tags ntt, fft, pigeonhole principle, meet in the middle, cses, solution

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en8 English abner_vidal 2026-04-07 03:38:33 2268
en7 English abner_vidal 2026-04-07 03:23:25 29
en6 English abner_vidal 2026-04-07 03:18:19 57 Tiny change: '# Equal-Sum Disjoint Subsets: An FFT-Based Approach\n\n## Introdu' -> '## Introdu'
en5 English abner_vidal 2026-04-07 03:17:57 13886 (published)
en4 English abner_vidal 2026-04-07 03:10:17 10
en3 English abner_vidal 2026-04-07 03:06:17 30
en2 English abner_vidal 2026-04-07 03:03:58 18243
en1 English abner_vidal 2026-04-07 02:55:01 4285 Initial revision (saved to drafts)