Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 D

Counting ways to pay each amount 0..9 with coins [1, 4, 6]: version A loops over amounts (outer) and coins (inner), counting ordered sequences of coins. Version B loops over coins (outer) and amounts (inner), counting unordered selections. Both start with d[0] = 1. At which amount index do the two tables first differ? Answer −1 if they agree everywhere.

  1. A. 5
  2. B. 6
  3. C. 4
  4. D. 7

Correct answer: A. 5

Only the ordered version counts 1 + 4 and 4 + 1 as two ways. With coins as the outer loop each coin type is processed once, so the first amount that can be paid in two different orders is where the tables first disagree.

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

Exams that ask this topic