# An application on Bertrandís ballot theorem in C/C++

CC++Server Side ProgrammingProgramming

In Bertrand's original paper, he explains a proof depended on a general formula for the number of favourable sequences implementing a recursion relation.

## Example

Let there are 5 voters, of whom 3 vote for candidate A and 2 vote for candidate B (so p = 3 and q = 2). Ten possibilities are exist for the order of the votes cast −

• AAABB

• AABAB

• ABAAB

• BAAAB

• AABBA

• ABABA

• BAABA

• ABBAA

• BABAA

• BBAAA

For the order AABAB, the tally of the votes as the election progresses is given below −

CandidateAABAB
A12233
B00112

For each column the tally for A is always greater than the tally for B so the A is always strictly ahead of B. For the order AABBA the tally of the votes as the election progresses is given below −

CandidateAABBA
A12223
B00122

With respect of this order, B is tied with A after the fourth vote, so A is not always strictly ahead of B. Of the 10 possible orders, A is always ahead of B only in case of AAABB and AABAB. So the probability that A will always be strictly ahead is 2/10=1/5 and this is indeed equal to 3-2 / 3+2 as the theorem predicts.