SECTION 03 / 17

Count pairs without counting twice

1 min read
Five objects have ten distinct unordered pairs. Ignore self-pairs and keep only one direction for each pair.
Five objects have ten distinct unordered pairs. Ignore self-pairs and keep only one direction for each pair.

Start with n × n choices. Remove the n self-comparisons. There are now n(n−1) ordered pairs. A–B and B–A represent the same unordered pair for birthday equality, so divide by 2.

Distinct unordered pairs=n(n−1)2\text{Distinct unordered pairs}=\frac{n(n-1)}{2}
n objectsPairs
10
21
510
936
30435
1004950
1000499500
1000049995000

Another way to see the formula is (n−1) + (n−2) + … + 1. For five cards this is 4 + 3 + 2 + 1 = 10. We also write this as “n choose 2”: choosing two different items without caring about their order.

Only halve reversed pairs when reversing the two items means the same test. “X is older than Y” has a direction; do not automatically treat every pair problem as symmetric. The pair totals here assume a complete scan without early stopping.

As n grows, the pair count grows roughly with n squared. Doubling a large n makes the work roughly four times as large. A fast computer still benefits from avoiding unnecessary work. Exact running time depends on the machine and what each comparison does; a fixed comparisons-per-second claim is only an illustration.

Try it yourself

Why is 30 × 30 ÷ 2 = 450 not the exact answer?

Show explained answer

It halves the self-comparisons too instead of removing them first. Use 30 × 29 ÷ 2 = 435.

Try it yourself

How many new comparisons does the final card make in the “later cards only” method?

Show explained answer

Zero. Its pair with every earlier card has already been checked.

Try it yourself

For 100 objects, is the exact pair count 10,000?

Show explained answer

No. That includes self-pairs and reversed pairs. The exact distinct unordered pair count is 100 × 99 ÷ 2 = 4,950.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑