In competitive programming, where people solve tough problems with computer code, the ** Pigeonhole Principle **is like a secret tool. Even though it’s a simple idea, it helps programmers tackle complex challenges. This article is your guide to understanding how this principle works and why it’s crucial for competitive programmers. Let’s explore this powerful tool that turns ordinary coders into problem-solving wizards!

Table of Content

__What is the Pigeonhole Principle?__

If you have more

thanpigeonsinto which you want to distribute thosepigeonholes, at least onepigeonsmust contain more than onepigeonhole.pigeonIn other words, if you have

objects andncontainers, wherem, then at least one of then > mcontainers must contain more than one object. This principle is named after the analogy ofm(objects) being placed inpigeons(containers).pigeonholes

**Key Points of Pigeonhole Principle:**

**Key Points of Pigeonhole Principle:**- It provides a simple way to prove the existence of certain arrangements or occurrences without explicitly finding them.
- It’s often used to establish the existence of duplicates, repetitions, or patterns within a set of objects or elements.
- The principle is a useful tool in various areas of mathematics, computer science, and problem-solving, including combinatorics, graph theory, and competitive programming.

__How to Identify ____Pigeonhole Principle____ Problems ?__

First, read the problem description carefully. Look for keywords that may indicate the presence of the Pigeonhole Principle, such as “divisible”, “distribution,” “buckets,” “categories,” “objects,” or “elements.”**Observe the problem statement carefully:**Distribution is a common indicator of the pigeonhole problem. Any problem requiring to distribution of elements(pigeons) into buckets (pigeonhole) may involve the pigeonhole principle.**Determine the distribution requirement:**Analyze the constraints of the task, including the number of objects and classes (or pigeons). If the number of objects exceeds the number of categories(pigeonholes) , this is a strong indication that the Pigeonhole Principle can be applied.**Analyze Constraints:****Look for Repetitions:****Compare with known pigeonhole problems:**In a group of 23, there is an even greater chance that at least two people have the same birthday. This intuitive result is an application of the pigeon principle.**Birthday Paradox:**If the dresser has more socks than drawers, at least one drawer should have more than one sock.**Socks in drawers:**

Lets discuss some problems that will help you to get a idea about Pigeonhole Principle:

**Problem 1:**

Suppose there are ** n** pairs of socks in the drawer, picking

**socks guarantees that at least one pair is chosen.**

**n+1**The Pigeonhole Principle is applied as follows:

In this case the individual socks we will have to pick will be the pigeons in this case.Pigeons:

A pair of socks consists of two socks of the same color, will be the pigeonholes in this case, There will be totalPigeonholes:pigeonholes.n

Now we pickMarking the Pigeonholes and Applying Pigeonhole Principle:socks. In the worst-case we will pick one sock from each pair . This means we will selectn+1different socks, one from each pair, and they will go into separate pigeonholes. Thus, all pigeonholes will be marked.nNow when we pick

sock, we will have to put it in one of existing pigeonholes which is already marked, thus the(n+1)thsock must necessarily match one of the previously chosen socks.(n+1)thSo, by applying the pigeonhole principle, we can conclude that picking

socks fromn+1pairs guarantees that at least one pair of socks is chosen.n

**Problem 2:**

Suppose ** m** and

**are relatively prime positive integers, and**

**n****and**

**0 â‰¤ a < m****. We want to show that there exists an integer**

**0 â‰¤ b < n****such that**

**x****and**

**x mod m = a****.**

**x mod n = b**The Pigeonhole Principle is applied as follows:

Consider the integersPigeons:, each of these integers gives remaindera, a + m, a + 2m, …, a + (n – 1)mwhen divided bya. Letmr_{0 }= a mod n, r_{1}= (a+m) mod n, r_{2}= (a+2*m) mod n , ……. , r_{n-1 }be the remainders when the above integers are divided by= (a+(n-1)*m) mod n. We want to prove that one of these remainders will be equal will give equal tom, which will solve our problem. The abovebremainders will be our pigeons in this case.n

Let us proof the statement by contradiction. ConsiderPigeonholes:boxes with numbern-1. This will be the pigeonholes in this case. We will put the above remainder0,1,2,3,â€¦,bâˆ’1,b+1,â€¦nâˆ’1rinto these boxes. We have not consider box_{i}because we will try to show no suchbexistsx

Since there areApplying Pigeonhole Principle:remainders (pigeons) andnboxes (pigeonholes), one box will contain two remainders. Let those two remainders ben-1rand_{i }r_{j}, thusr_{i}=r_{j}=> r

_{i}= a + i*m – q1*n, and=> r

_{j}= a + j*m – q2*nSince r

_{i}=r_{j },=> a + i*m – q1*n = a + j*m – q2*n

=> (j-i)* m= (q2- q1)*n,

This means thatÂ n divides (j-i).

But sinceÂ

Â andÂiÂ are distinct there values lie inÂj,Â{0,1,2,â€¦,nâˆ’1}

=>, this leads to a contradiction because0 < (j – i) < ncannot divide a positive integer smaller than itself. Therefore,ndoes not dividen.(j – i)Hence, two remainders cannot belong to same box. So one the of remainder will have to belong to box

, which implies one of the remaindersbr_{0}, r_{1 }, ……. , rwill be equal to_{n-1 }.b

**Problem 3:**

Given an array of ** n** integers, where

**is a positive integer. the task is to determine whether there exists a non-empty subset of the given array whose sum is divisible by**

**n****.**

**n**The Pigeonhole Principle is applied as follows:

The prefix sums of the array are calculated, and for each prefix sum, the remainder when divided byPigeons:is computed. These remainders are the “pigeons” in our problem.n

The “pigeonholes” correspond to the distinct remainders that can occur when dividing byPigeonholes:(i.e., 0 to n-1).n

An arrayMarking the Pigeonholes:is used to mark which “pigeonhole” (remainder) are occupied. While iterating through the array of prefix sum if remainder of current prefix has already been marked inremainderExists, then answer will beremainderExists.yes

The answer is always “YES” to this problem because of the Pigeonhole Principle. The number of distinct remainders when dividing any integer by ** n** ranges from

**to**

**0****, creating**

**n-1****possible “pigeonholes.” In contrast, when you calculate the prefix sums of an array, you start with a sum of**

**n****, and you have**

**0****additional remainders (from 0 to n-1) in the prefix sum array. This means you have**

**n****“pigeons” to place into the**

**n+1****“pigeonholes.”**

**n**By the Pigeonhole Principle, if you have more “pigeons” than “pigeonholes,” at least one “pigeonhole” must contain more than one “pigeon.” Therefore, there must exists a index in prefix sum array such that remainder(prefixsum[i]%n) has already been marked in ** remainderExists** array. Therefore, there must be a subset of the array whose sum is divisible by

**.**

**n**__How to solve problems of Pigeonhole Principle?__

1. Determine the Pigeons and Pigeonholes:After Identifying the problem statement carefully, you need to determine the pigeons and pigeonholes in the problem. Pigeons are the objects which need to be distributed and pigeonhole corresponds to buckets into which pigeons will be distributed. As in the above-solved problem, the remainders were the pigeons in the problem and the pigeonholes corresponded to the distinct remainders that can occur when dividing by

.n

2. Apply the Pigeonhole Principle:Once the pigeons and pigeonholes have been identified, the pigeonhole principle can be applied. The Pigeonhole Principle states that if you have more pigeons than pigeonholes, then at least one pigeonhole must have more than one pigeon. Apply this to your problem by figuring out if there are more holes than holes. If your problem setup matches this, it means you can apply the pigeonhole principle.