IAE Academy

GATE CS

Combinatorics

Counting

Counting is the art of finding how many objects have a given property without listing them: choosing, arranging, distributing and avoiding forbidden cases. It gives closed forms for the number of strings, subsets, functions, relations, matrices and lattice paths, and the sample-space sizes behind probability questions. A small set of rules and identities, applied with care about whether order matters and whether repetition is allowed, settles such questions.

6 previous-year questions · 6 solved · 6 practice questions

1. Basic rules, permutations and combinations

Sum rule. If a task can be done in one of two ways that cannot happen together (disjoint cases), with and outcomes respectively, there are outcomes. Product rule. If a task is done in two stages, with outcomes for the first and for the second whatever the first was, there are outcomes. Most formulas below are built from these two rules.

Two questions decide the formula: does order matter (arrangement or selection), and is repetition allowed? Here is the number of distinct kinds available and the number of positions or picks. Conventions used throughout: is the number of elements of a finite set ; ; ; and when or .

Choose r from n distinct kinds

Repetition not allowed

Repetition allowed

Order matters (arrangements)

Order does not matter (selections)

PERMUTATIONS OF R FROM N DISTINCT OBJECTS

Ordered arrangements, no repetition, . Also the number of injective functions from an -set to an -set.

COMBINATIONS

Unordered selections of from distinct objects, no repetition, . Symmetry: choosing the ones taken is the same as choosing the ones left.

SELECTIONS WITH REPETITION

Unordered choice of items from kinds () when each kind may be taken any number of times (multisets of size ).

CIRCULAR ARRANGEMENTS

Seating distinct people round a table when rotations count as the same arrangement (reflections counted as different). If reflections are also the same and , divide by 2: .

Procedure for a counting question

  1. Decide what is being built (a string, a subset, a function, a seating) and what makes two outcomes different.

  2. Break the construction into stages; use the product rule across stages and the sum rule across disjoint cases.

  3. For a restriction, either build so the restriction is respected, or count all and subtract the bad ones (complement).

  4. Check with a tiny case (n = 2 or 3) by listing.

EXAM TIP

Place the restricted position first. For a number whose first digit cannot be 0, fill the first digit (9 ways), then the rest freely. Choosing a leading position last often forces awkward case splits.

2. Identical objects, multinomials and distributions

When some objects are identical, swapping them gives the same arrangement, so you divide out those swaps. For objects with of one kind, of another and so on, where , the count is the multinomial coefficient.

ARRANGEMENTS WITH IDENTICAL ITEMS (MULTINOMIAL COEFFICIENT)

Distinct arrangements of objects of which are alike of type , . It is also the number of ways to split distinct objects into labelled groups of sizes .

STARS AND BARS: NON-NEGATIVE SOLUTIONS

Number of integer solutions (); equals the number of ways to put identical objects into distinct bins, empty bins allowed.

STARS AND BARS: POSITIVE SOLUTIONS

Every bin non-empty (). For a lower bound , substitute and reduce by .

STARS AND BARS WITH AN INEQUALITY

Add a slack variable ; the equation now has unknowns summing to .

For distinct objects, each object independently chooses a bin, so there are ways to place distinct objects into distinct bins. The number of ways to place them so that no bin is empty (onto functions) comes from inclusion-exclusion, covered in section 4. Dividing that by gives the number of ways into identical non-empty bins, called the Stirling number of the second kind .

STIRLING NUMBERS OF THE SECOND KIND

= number of ways to split a set of distinct objects into non-empty unlabelled blocks. Conventions: , for , for . Also . Example values: , , .

PARTITIONS OF AN INTEGER INTO EXACTLY K PARTS

Number of ways to write as a sum of exactly positive integers with order ignored. Boundary values: ; if , or if and . is the total over all ; .

Objects

Bins

Rule

Count

distinct

distinct, empty allowed

each object picks a bin

distinct

distinct, none empty

onto functions

identical

distinct, empty allowed

stars and bars

identical

distinct, none empty

stars and bars

distinct

identical, none empty

Stirling number

distinct

identical, empty allowed

sum over non-empty bins used

identical

identical, none empty

integer partitions

identical

identical, empty allowed

partitions into at most parts

WORKED EXAMPLE

How many distinct arrangements of the letters of ENGINEER have no two E's adjacent?

Solution
  1. ENGINEER has 8 letters: E three times, N twice, and G, I, R once each.

  2. Arrange the five non-E letters N, N, G, I, R first: ways.

  3. These five letters leave 6 gaps (including both ends). Put the three E's in three different gaps: ways.

  4. The E's are identical, so no further division is needed. Total .

Answer: 1200

EXAM TIP

Do not confuse the two stars-and-bars forms. With the top of the binomial is ; with it is . Test with : non-negative gives 3 solutions (0+2, 1+1, 2+0), positive gives 1.

3. The binomial theorem and identities

BINOMIAL THEOREM

For a non-negative integer . The coefficient of is ; there are terms. In the coefficient of , where , is .

PASCAL'S IDENTITY

For and . Split the -subsets of an -set by whether they contain a chosen element.

SUM OF A ROW

Total number of subsets of an -set; from and . Hence even-size subsets and odd-size subsets each number for .

WEIGHTED ROW SUM

Follows from (choose a committee, then its leader).

VANDERMONDE'S IDENTITY

Choose from two groups of sizes and by how many come from the first (terms with or are 0). The case gives .

HOCKEY-STICK IDENTITY

For . Sum down a diagonal of Pascal's triangle.

Within row the entries rise strictly up to the middle and fall strictly after it. For even the single largest entry is ; for odd the two middle entries and are equal and both are the largest. Many identities have a combinatorial proof: count one set in two different ways.

EXAM TIP

To evaluate a sum like , recognise the binomial theorem with : the value is . Putting numbers into is the fastest way to get such sums.

4. Inclusion-exclusion, derangements and the pigeonhole principle

INCLUSION-EXCLUSION

Counts elements with at least one of properties; for two sets, . The number with none of the properties is minus this value.

Use it in the complement form for restrictions: let be the set of objects that violate condition ; the good objects number . When every -fold intersection has the same size by symmetry, each sum collapses to times one term.

ONTO FUNCTIONS

Functions from an -set onto a -set; equals and is if .

DERANGEMENTS

Permutations of objects with no object in its original place. Convention . Also , and for , is the integer nearest to . .

PERMUTATIONS WITH EXACTLY K FIXED POINTS

Choose which objects stay put, then derange the other (for this gives , the identity).

PIGEONHOLE PRINCIPLE (GENERALISED)

For positive integers , . To guarantee at least objects in one of boxes you need objects. It proves existence only; it does not construct the box.

WORKED EXAMPLE

Find the number of integer solutions of with for every .

Solution
  1. Without the upper bounds: .

  2. Let be the solutions with . Put : the sum becomes 8, giving . There are 4 choices of , so .

  3. Two variables : the sum becomes , giving , for pairs: total 24.

  4. Three variables would need at least 21 > 15, so those terms are 0.

  5. Good solutions .

Answer: 180

EXAM TIP

In a pigeonhole question work out the worst case first: fill every box to one below the target, then one more object forces the result. For 12 boxes and a target of 4 in one box, the worst case is , so 37 objects are needed.

5. Strings, matrices, lattice paths and Catalan numbers

Many counting questions ask for the number of strings, functions, relations or matrices with a property. Reduce each to choosing positions or entries, as below. By the product rule, a string of length over an alphabet of symbols has possibilities in all. A relation on a set is a subset of , so a relation on an -set is the same as an 0/1 matrix.

Object

Count

Strings of length over symbols

Binary strings of length with exactly ones

Subsets of an -set

Pairs of subsets of an -set with

Functions from an -set to an -set

Injective functions from an -set to an -set

Bijections between two -sets

binary matrices (relations on an -set)

Reflexive relations on an -set (also irreflexive)

Symmetric relations on an -set

Reflexive and symmetric relations on an -set

Antisymmetric relations on an -set

Asymmetric relations on an -set

Equivalence relations on an -set

Bell number

permutation matrices (one 1 in each row and column)

binary matrices with no all-zero row

BELL NUMBERS

Number of partitions of an -set into non-empty blocks, which is also the number of equivalence relations on it (each relation corresponds to its set of equivalence classes).

LATTICE PATHS

Shortest paths from to using unit steps right and up: choose which of the steps are right. Forced points are handled by multiplying path counts of each leg.

CATALAN NUMBERS

Paths from to with right steps (along ) and up steps (along ) that never go above the line (touching allowed). Also: balanced bracket strings with pairs; unlabelled binary trees with nodes (left and right child distinguished); binary search trees on distinct keys; full binary trees with leaves; ways to parenthesise a product of factors; triangulations of a convex -gon.

BINARY STRINGS WITH NO TWO CONSECUTIVE 1S

Condition on the end: a valid string ending in 0 is any valid string of length followed by 0; a valid string ending in 1 must end in 01, after any valid string of length . Values: 2, 3, 5, 8, 13, 21, 34, 55 for ; with .

K-SUBSETS WITH NO TWO CONSECUTIVE ELEMENTS

Number of -element subsets of with no two consecutive integers; equivalently binary strings of length with ones, no two adjacent (place the ones in the gaps around the zeros).

The restricted-path count behind the Catalan formula uses reflection: paths from to that go above touch the line ; reflecting the part after the first touch in that line gives a one-to-one match with all paths to , which number . Subtract them from all paths .

WORKED EXAMPLE

How many binary matrices have exactly two 1s in every row and exactly two 1s in every column?

Solution
  1. With only the row condition, each row is one of patterns, so there would be matrices.

  2. Take the complement (swap 0 and 1). A matrix has exactly two 1s in every row and column if and only if its complement has exactly one 1 in every row and column.

  3. A matrix with exactly one 1 in every row and column is a permutation matrix; there are of them.

  4. Complementing is a one-to-one correspondence, so the answer is 6.

Answer: 6

EXAM TIP

The Catalan formula needs the path to stay on or below the diagonal (touching allowed). If the path may touch the diagonal only at its two ends, the count is , not : the first step must be right and the last step up, and the part in between is a Catalan path of size below the line . The same number of paths lie strictly above the diagonal.

PREVIOUS-YEAR QUESTIONS

6 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2026 Set 1 · GATE CSE 2025 Set 1 · GATE CSE 2025 Set 2 · GATE CSE 2023 · GATE CSE 2022 · GATE CSE 2020

GATE 2025 CS2 Q.64

GATE 2025 · CS2Q.64 · 2 marks · NAT

A quadratic polynomial over complex numbers is said to be square invariant if . Suppose from the set of all square invariant quadratic polynomials we choose one at random.

The probability that the roots of the chosen polynomial are equal is ______. (rounded off to one decimal place)

Answer and solution
See the question in the paper
GATE 2025 CS2 Q.64: answer and solution

GATE 2023 CS Q.48

GATE 2023 · CSQ.48 · 2 marks · MCQ

Let , where is a large positive integer greater than 1000. Let be a positive integer less than . Let be subsets of with and . We say that a permutation of separates from if one of the following is true.

  • All members of appear in the permutation before any of the members of .
  • All members of appear in the permutation before any of the members of .

How many permutations of separate from ?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution
See the question in the paper
GATE 2023 CS Q.48: answer and solution

OTHER EXAMS

ISRO: 1 question

From ISRO's Scientist/Engineer written test: ISRO CS 2025. They are not counted in the GATE numbers on this page.

PRACTICE

6 questions to test yourself

PRACTICE 11 mark · MCQ

Five letters are written to five different people and the five addressed envelopes are prepared. The letters are put into the envelopes, one per envelope. In how many ways can exactly two letters land in their own correct envelopes?

  1. (A)

    20

  2. (B)

    30

  3. (C)

    45

  4. (D)

    44

Answer and solution

Answer: (A)

  1. Choose the 2 letters that are correctly placed: .

  2. The other 3 letters must all be misplaced: .

  3. Total .

PRACTICE 22 marks · NAT

A path goes from to using unit steps to the right or upward only. How many such paths never go above the line (touching the line is allowed)?

Answer and solution

Answer:

  1. This is the Catalan count for .

  2. .

PRACTICE 32 marks · MCQ

How many onto (surjective) functions are there from a set of 5 elements to a set of 3 elements?

  1. (A)

    243

  2. (B)

    150

  3. (C)

    120

  4. (D)

    60

Answer and solution

Answer: (B)

  1. Use inclusion-exclusion over the elements of the codomain that are missed.

  2. Number .

  3. Check: .

PRACTICE 42 marks · MSQ

For , which of the following statements are true?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

    The number of subsets of a 6-element set with an even number of elements is 64

Answer and solution

Answer: (A), (B), (C)

  1. A: the row sum is , true.

  2. B: , true.

  3. C: , true.

  4. D: even-size subsets number , not 64, so false.

PRACTICE 51 mark · NAT

What is the smallest number of people a group must have to be certain that at least 4 of them were born in the same calendar month?

Answer and solution

Answer:

  1. The months are 12 boxes; a target of 4 in one box.

  2. Worst case: each of the 12 months has exactly 3 people, which is 36 people with no month reaching 4.

  3. One more person forces some month to have 4: , the formula with , .

PRACTICE 62 marks · NAT

How many binary strings of length 8 contain no two consecutive 1s?

Answer and solution

Answer:

  1. Let be the count for length : , and .

  2. , , , , , .

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

Combinatorics: counting

Preparing in Kolkata? IAE Academy's course for GATE CSE.