Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many strings of length 5 over the alphabet {0, 1, 2} have a digit sum divisible by 3 (the sum of their digit values)?

  1. A. 84
  2. B. 81
  3. C. 82
  4. D. 27

Correct answer: B. 81

Count by DP over the residue of the running sum mod 3: each new digit shifts the residue by 0, 1 or 2, so the three residue counts evolve linearly. The total 3ⁿ divided by 3 is only an estimate, since residues need not be equally likely at every length.

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

Exams that ask this topic