Data Structures & AlgorithmsModerate

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

  1. A. 54
  2. B. 48
  3. C. 24
  4. D. 78

Correct answer: C. 24

Pick the first value in m ways and each later value in m−1 ways, giving m·(m−1)^(n−1). Multiplying the (m−1) choice onto the wrong position, or subtracting only the constant sequences, gives the other totals.

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

Exams that ask this topic