Data Structures & AlgorithmsModerateTCS NQT · Infosys

How many arrays of length 4 with entries from {1, 2, 3} have no two adjacent entries equal?

  1. A. 81
  2. B. 48
  3. C. 24
  4. D. 21

Correct answer: C. 24

The first entry has 3 choices and each later entry has 2 (anything except the previous one), so the count is 3 × 2³ = 24. Allowing equal neighbours gives 3⁴, and forgetting that the rule restricts each step to 2 choices gives 3 × 2⁴.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic