So i was trying to solve this problem.

```
You are given a multiset of N integers. Please find such a nonempty subset of it that the sum of the subset's elements is divisible by N. Otherwise, state that this subset doesn't exist.
```

What i did was simply generate the combinations and check if each of them was divisible by its cardinality. This solution turned out just fine.

After the competetion i found an elegant solution here.

It so turns out that i cannot generate a sequence of *n* numbers without having a **consecutive** subset which is not a multiple of *n*.

That is to say, whatever combination of numbers i try..there always exists a consecutive subset which is a multiple of 'No. of numbers'(*n*).

Can anyone explain this theoritically? Or is there any theorem that states this?

PS: I already tried Googling it...all in vain.

## Comments

Sort by:

TopNewestThis is a classic application of Pigeonhole. Let the terms be \(a_1\), \(a_2\), \(\dots\), \(a_n\). Consider the \(n\) sums \[ \begin{align*} s_1 &= a_1, \\ s_2 &= a_1 + a_2, \\ &\dots, \\ s_n &= a_1 + a_2 + \dots + a_n. \end{align*} \] If any of these sums are divisible by \(n\), then we are done.

Otherwise, each of these sums are congruent to one of 1, 2, \(\dots\), \(n - 1\) modulo \(n\). There are \(n\) sums and \(n - 1\) possible residues modulo \(n\), so by Pigeonhole, two residues must be the same. In other words, there exist \(i\) and \(j\), \(i < j\), such that \(s_i \equiv s_j \pmod{n}\). Then \[s_j - s_i = a_{i + 1} + a_{i + 2} + \dots + a_j\] is divisible by \(n\), as desired.

Furthermore, the example \(a_1 = a_2 = \dots = a_{n - 1} = 1\) shows that you need at least \(n\) terms to guarantee that you can find a sum of consecutive terms that are divisible by \(n\). – Jon Haussmann · 2 years, 4 months ago

Log in to reply

– Shaan Ragib · 2 years, 4 months ago

niceLog in to reply

@Sreejato Bhattacharya remember that Pigeonhole trick/thing you showed me? By the way I'm totally showing that to the class. :D – Finn Hulse · 2 years, 4 months ago

Oh. I first though to apply Pigeonhole to a different part of the problem.Log in to reply

– Sreejato Bhattacharya · 2 years, 4 months ago

Cool! :)Log in to reply

not a question – Rohan Lad · 2 years, 4 months ago

Log in to reply

Interesting problem. Possibly a Pigeonhole argument is needed? – Calvin Lin Staff · 2 years, 4 months ago

Log in to reply