Skip to content
Official PYQ · 2003

GATE CS 2003 Q61 — Searching, Sorting, Hashing

Explain with AI

In a permutation a1...ana_1 ... a_n, of n distinct integers, an inversion is a pair (ai,aj)(a_i, a_j) such that i<ji < j and ai>aja_i > a_j.

If all permutations are equally likely, what is the expected number of inversions in a randomly chosen permutation of 1...n1. . . n?

Choose one option
View worked solution

Answer: B

To find the expected number of inversions in a randomly chosen permutation of nn distinct integers, we can use the linearity of expectation.

Let the permutation be a1,a2,…,ana_1, a_2, \dots, a_n. An inversion is a pair (ai,aj)(a_i, a_j) such that i<ji < j and ai>aja_i > a_j.

For any pair of indices (i,j)(i, j) with 1≤i<j≤n1 \le i < j \le n, let XijX_{ij} be an indicator random variable defined as:

Xij={1if ai>aj0otherwiseX_{ij} = \begin{cases} 1 & \text{if } a_i > a_j \\ 0 & \text{otherwise} \end{cases}

Since all permutations are equally likely, by symmetry, the probability that ai>aja_i > a_j is exactly 12\frac{1}{2} (as it is equally likely that ai>aja_i > a_j or ai<aja_i < a_j).

Therefore, the expected value of each indicator variable is:

E[Xij]=1⋅P(ai>aj)+0⋅P(ai<aj)=12E[X_{ij}] = 1 \cdot P(a_i > a_j) + 0 \cdot P(a_i < a_j) = \frac{1}{2}

The total number of such pairs (i,j)(i, j) with i<ji < j is given by the combination (n2)\binom{n}{2}:

(n2)=n(n−1)2\binom{n}{2} = \frac{n(n-1)}{2}

Let XX be the total number of inversions, so X=∑1≤i<j≤nXijX = \sum_{1 \le i < j \le n} X_{ij}. By the linearity of expectation:

E[X]=E[∑1≤i<j≤nXij]=∑1≤i<j≤nE[Xij]E[X] = E\left[ \sum_{1 \le i < j \le n} X_{ij} \right] = \sum_{1 \le i < j \le n} E[X_{ij}] E[X]=n(n−1)2×12=n(n−1)4E[X] = \frac{n(n-1)}{2} \times \frac{1}{2} = \frac{n(n-1)}{4}

Thus, the expected number of inversions is n(n−1)4\dfrac{n(n-1)}{4}.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions