No formulas are required to use the The Pigeon House Principle.
See the following examples.
Imagine that I have 3 pigeon houses. If I own 4 pigeons, then surely in some house there will be more than one pigeon. If the number of pigeons is greater than the number of houses, there will certainly be some house with more than one pigeon. It was by thinking of examples like this that the principle is called The Pigeon House Principle.
Imagine I have black, brown, white and gray socks. A certain day there was no light in my house and I need to remove the minimum amount of socks to ensure that there will be AT LEAST two socks of the same color. Now, let’s think of the worst-case scenario, which is to say, think of yourself as the unluckiest person in the world.
There are 4 colors possible. Therefore, 4 socks are not enough, as I could remove a black sock, a brown, a white and a gray. But, in the fifth half has no way to escape: it must repeat some of the colors mentioned. So with 5 socks I am sure I will have AT LEAST one pair of socks of the same color. It may even be that I have (luckily) more than two socks of the same color, but AT LEAST two I guarantee.
In the vast majority of problems, you have to imagine that you are the unluckiest person in the world, you have to think about the worst of the odds.
Let’s take one more example.
In a drawer, there are 4 black socks, 2 white socks, 8 gray socks. What is the minimum amount of socks I need to remove from this drawer to ensure I have at least two socks of different colors?
Let’s think about the worst case scenario? Well, if I want to remove two socks of different colors, the bad luck is to get several socks of the same color. As I am VERY UNLUCKY, I start to pick up half-ashes (because it is the one that has the most amount). I’m so unlucky I catch eight gray socks consecutively.
After I catch 8 half ashes, there is no escape. The next half has to be of another color. Therefore, 9 socks is the minimum amount of socks to ensure that we will have at least two socks of different colors. It may even be that of the 9 socks I have more than two socks with different colors, but this is luck and not sure.
One more example.
How many people need to be in an auditorium to be sure (I said SURE!!!) that at least two of them have birthdays on the SAME day?
I don’t mean that they were born in the same year, just that they have the same birthday.
Before I write the answer, I want to think a moment together with you (if you haven’t answered it by yourself). Let’s see: if there are two people, there is obviously no guarantee that they will both have one birthday on the same day. Most likely, it is not so. But in addition to probable (or not probable), the fact is that we are looking for CERTAINTY. And having two people in the auditorium we could never be sure that both were born on the same day.
The same would happen if there were three people. Or even ten. Or fifty. No? Or a hundred. Or two hundred. Or even three hundred!!! Why? Now, because although with 300 people in an auditorium it is likely that there will be two who celebrate their respective birthdays on the same day, we still cannot ensure or guarantee that what we want is right. It is that we could have the BAD LUCK that everyone had been born on different days of the year. We are approaching an interesting point in the conversation...
Because if there were 365 people in the auditorium, we still wouldn’t be able to ensure that two of them have the same birthday. It could happen that they were all born on every possible day of a year. Even worse, not even 366 (because of leap years). It may be that the 366 people in the auditorium cover exactly every possible day of a year without repetition.
However, there is a categorical argument: if there are 367 people in the auditorium, there is no way out: at least two have to have a birthday on the same day.
Of course, we do not know what these people are, nor if there are more than two who attend to the property requested. There may be more... much more, but that doesn’t interest us. The guarantee is that with 367 people, we solve the problem.
source
Not complicating ? If each sock has a pair when you take at least
N+1
is guaranteed to have a pair, summarizing your entire code tocout << (N + 1);
– Isac
Thank you Isac was that right
– Beginner Programmer
I really messed up the situation
– Beginner Programmer